5. Insert a line comment to identify each step. 6. View this video: View this video: Opening a File in Read Mode NOTES: For Python, "file" is the object that will provide basic functions and methods required to manipulate files. You need to use open() before you can read, append or write to a file. The open() function will open a file and the filename is the name of the file you want opened. The mode refers to how the file will be opened: " r " for reading, " w " for writing and " a " for appending. The open() function takes two arguments: the name of the file and the mode to use to open the file. (if no mode is indicated, the default is " r " or read). open() - will open files in your system - the filename is the name of the file to be opened. - The open() function will open a file and the filename is the name of the file you want opened. - The mode refers to how the file will be opened: " r " for reading, "w" for writing and "a" for appending. - The open() function takes two arguments: the name of the file and the mode to use to open the file. (if no mode is indicated, the default is " r " or read) fileHandling = open("Hello.txt", "r") read() - contains different methods: read(), readline() and readlines() - read() will return one big string fileHandling = open("Hello.txt", "r") print(fileHandling.read()) - readline() will return one line at a time filcHandling = open("Hello.txt", "r") print(fileHandling.readline()) - readlines( ) will return a list of lines fileHandling = open("Hello.txt", "r") print(fileHandling.readlines()) Write 0 - writes a sequence of strings to the file - write() is used to write a fixed sequence of characters to a file fileHandling = open("Hello.txt", "w") write("Hello World") fileHandling.close() - writelines () will write a list of strings fileHandling = open("Hello.txt", "w") linesOfText = ["a line of text", "another line of text", "a third line"] fileHandling. writelines(linesOfText) fileHandling.close()
Append () - will append to the file instead of overwriting it - To append an existing file, simply open the file in append mode ("a"): fileHandling = open("Hello.txt", "a") write("Hellow World again") fileHandling.close() Close() - will free up system resources used by the open file. Always close when you are finished with a file fileHandling = open("Hello.txt", "r") print(fileHandling.read()) fileHandling.close() 7. Create a text file and name it poem1 with file extension .txt a. Search your computer for the application Notepad or Notepad ++ b. Open it c. Copy and paste the following 7 line poem: Loops I repeat loops loops loops I repeat until I break d. Click File, Save As e. Key file name: poem1 f. Save as type: Select from the drop down normal text file (?.xt) g. Verify that you are saving the file in your Python course folder so you will remember how to find it. h. Click Save 8. Add your file created above to your IDE. Review the video if you have problems getting the file into your IDE. repl.it users: a. At the top left you will see the word "Files". To the right of that you will see an ellipsis ( 3 dots). Click on the 3 dots. b. Select upload file. Then locate your file and select it. c. OR you can drag and drop your folder into the navigation pane under Files. Examples: Key and run for understanding open() creates an object that can be addressed in python code. 3. Open the file in memory as poemFile. Note: be sure you have
added the poem 1 text file in the project files for this program. \# This code should display poeml with no errors. You will need to use read() poemFile = open ('poem 1.txt ', ' r ') print(poemFile.read ()) Import and open a local file in Read Mode 10. Create a txt file called cities. Copy and paste the following data into it: Beijing Cairo London Nairobi New York City Sydney Tokyo 11. Import the cities text file into your IDE (see above if you have trouble). 12. Open citics.txt in read mode using citicsFile. Print citiesFile and ensure the file displays correctly with no errors, i.c.: use .read() with citiesFile -- see example above. 13. View this video: Reading a File Concept: Reading Text Using read() read() loads the content of the file into memory as a string, including formatting such as new line (l) Examples \#review, key and run for understanding 14. poemFile = open ("poem1.txt", " r ") poemContents = poemFile.read () \# shows the file as a string with formatting characters such as "\n", output should be non-blank poemContents \# since ?read() loaded the file as a string it can be printed print(poemContents) Read a File 15. Read the cities.txt file that was imported in Steps 10/11 16. Verify that the cities.txt file is in your IDE 17. Read() citiesFile into a variable called cities 18. Test the read() by printing the string cities 19. View this video: Reading a File with .read(n) Concept: Read a File using .read(n) Where n= number of characters to read. Each time poemFile.read(10) runs, the next 10 characters are read. Note: if .read(10) result is = "" (an empty string with no characters), it is likely that the end of the file has been reached. Perform a fresh .open() to
reset read() to the beginning of the file. Examples \#review, key and run for understanding 20. \# read poem 1.txt10 characters at a time poemFile = open ('poem1.txt', 'r') poem 10Char= poemFile. read (10) print(poem10Char) \# read +10 more characters \# reads and displays without storing in a variable poemFile.read (10) \# read +10 more characters \#Reads and stores in variable poemParts poemParts = poemFile.read (10) print(poemParts) \# read +5 more characters each time run are appended using string addition \# Consider why no additional text displays after multiple runs poemParts += poemFile.read (5) print(poemParts) Digits of Pi 21. Create a digitsOfPi text file. Copy and paste the following data: 3.14159265358979323846264338327950288419716939937510 5820974944592307816406286208998628034825342117067982 1480865132823066470938446095505822317253594081284811 1745028410270193852110555964462294895493038196442881 0975665933446128475648233786783165271201909145648566 923460348610454326648213393607260249141273 22. Import digitsOfPi 23. Open as digitsOfPiText 24. Read()the first 4 characters of digitsOfPiText into piDigits. 25. Print piDigits. 26. Add to piDigits string with string addition. a. Add the next 4 characters for digitsOfPi obtained from read(). b. Repeat these 2 lines of code 3 more times to see more digits added to piDigits (each print should add 4 more digits to pi with the last print showing 18 digits) 27. View this video: Read Returns a String Concept: read() returns a string These strings can be manipulated just like any other string. Boolean tests: - upper() - title()
- String slices, e.g. - cities[3:9] - etc... String tests: - .isdigit() - .isalpha() - etc... Examples \#review, key and run for understanding 28. poemFile = open('poem1.txt', ' r ') poemPart = poemFIle.read(15). upper() print(poemPart) poemPart = poemFIle.read(6).title() print(poemPart) poemPart = poemFile.read(6) print(poemPart) print(poemPart.isalpha(), "isalpha() because of ' \ pn' '’) poemPart poemFile = open('poem1.txt', ' r ') poemText = poemFile.read () print(poemText[8:26]) City Initials 29. Verify that the cities text file is uploaded in your IDE 30. Read() citiesFile into cities 31. Iterate through the characters in cities a. Test if .isupper() b. If True, add the character to a string variable named: initials c. Else if character is " n " is True, add the "ln" to initials 32. Print initials