<<

Unit II - Focuses on Python structures such as strings, lists, and range sequences, as well as methods for working with these structures. Students will be introduced to data structures and files in Python 3 and be ready for - advanced Python learning.

Module IV – Working with Files Files .readlines() and .() Methods Students will be able to: • File import • Import files into IDE • File () • Open() and .() local text files in memory • Use .read() • .read() a specific number of characters • File Read as a list with .readlines() • Use .readlines() to read data from file as list • File Closing to free resources with .close() • Use .close to free system resources • File Read a line at a time with .readline() • Use.readline() to read data from file line by line • Remove characters using .strip() • Use .strip() to remove new line characters • File .() with .seek() • .write() data to a new local file • File append mode • Use .seek() to set file read or write location • Use file append mode

Activity 2 File .readlines() and close() Methods 1. Open Your IDE of Choice (Notebooks.Azzure, repl.It or Visual Studio 2. Open a New Project 3. Name it U2_M2_A2-XX (XX represents your first initial and last name I.E Mjordan). 4. Insert your program comments. 5. Insert a line comment to identify each step. 6. View this video: Readlines: Open Text as a List

Concept: .readlines() File read as a list with .readlines() – converts the lines of a file into a list of strings. poemLines = poem1.readlines()

Examples: # review, key and run for understanding 7. Verify the poem1 has been downloaded into your IDE. (see activity 1 to review this procedure) # open poem1 text file poem1 = open(‘poem1.txt’, ‘r’)

# readlines and print as a list poemLines = poem1.readlines() print(poemLines)

for line in poemLines: print(line)

Task 1 .readlines()

8. Verify the cities text file has been downloaded into your IDE. 9. Open cities.txt in read mode using a variable: citiesFile 10. Read citiesFile as a list variable: citiesLines using .readlines() 11. Use list iteration to print each city in citiesLines list 12. View this video: Readlines: Remove Characters Concept: Working with lists from .readlines() Remove characters from lists created using .readlines()

for line in poemLines: poemLines[count] = line[:-1] count +=1

line[:=1] sets the end point at the last character of the string, the result is the ‘\n’ (newline) character is omitted.

Examples # review, key and run for understanding 13. # re-open file and read file as a list of strings poem1 = open('poem1.txt', 'r') poemLines = poem1.readlines() print(poemLines)

# print each list item for line in poemLines: print(line)

# [ ] remove the last character of each list item, which is "\n" count = 0

for line in poemLines: poemLines[count] = line[:-1] count += 1 print(poemLines)

# print each list item for line in poemLines: print(line)

Task 2 Remove newline characters from cities lists created using .readlines() In Task 1, the cities text file was printed with a blank line between each city and Task 2 removes the blank lines.

14. Verify the cities.txt file is in your program. 15. Open cities.txt as citiesFile and read the file as a list: citiesLines. 16. Remove the last character, “\n”, of each citiesLines list item. 17. Print each list item in citiesLInes. (hint: use for in loop) 18. View this video: .close()

Concept: File .close() method frees resources File.close() method removes the reference created from file open() function, i.e.: Poem1.close() Examples: # review and run for understanding 19. Verify the poem1 text file has been downloaded to your program.

# Open and readlines of poem1.txt poem1 = open('poem1.txt', 'r')

# readLines breaks if file is no longer open poemLines = poem1.readlines() print(poemLines)

# Close poem1 poem1.close()

Task 3 File .close()

20. Write each item in its own cell: • Verify the cities text file has been downloaded to your program • Open cities.txt as citiesFile • Read the lines as citiesLines • Print the cities that start with the letter “D” or greater • Close citiesFile • Test that the file is closed

Task 4 readLines() poem2 21. Create a text file and name it poem2. The following data should be saved in poem2: while True I loop True loop True loop not True False End

22. Write each item in its own cell: • Verify the poem2 text file is downloaded to your program • Open poem2.txt as poem2File in read mode • Create a list of strings, called poem2Lines, from each line of poem2Text (use readlines()) • Remove the newline character for each list item in poem2Lines • Print the poem2Lines in reverse order 23. Thoroughly test your code, check comments and then submit according to teacher instructions. 24. Take the activity quiz.