Professional Documents
Culture Documents
LN_4_TextFiles (6)
LN_4_TextFiles (6)
PROGRAMMING PARADIGM
Script Programming with Python
- Text files
Text Files
File is a sequence of bytes stored under a filename
• Text file can be created with any word processor
• To read the file a line at a time, and returns a list of one-line strings
Text Files – Close File
• If files are not closed after opened, it will be closed by
Python after it’s no longer referenced
• However, files should be closed after execution to force any
remaining write to be completed
After the block of code under the context manager, the file is closed automatically.
Text Files – Altering Items in a Text File
• Altering, inserting, or deleting a line of a text file
• Cannot be made directly
* Look into seek() and tell() to keep track of where you are in the file
Text Files – Altering Items in a Text File
• Functions needed for these tasks must be imported from
standard library module os
• To remove a file
• To rename a file
Hands-on Activities
Open a blank file in your text editor and copy the following text into the file.