Reading, Writing, and Deleting Files
These are my Tech Notes for File IO in Python. A file can be opened in several modes, read, append, write, and create. It can also be specified as text or binary.
Reading a file
- A file can be opened in several modes read, append, write, and create. Create a variable name, and assign it the opened file to be manipulated. Create a file stream with read access and assign it to variable fh.
fh = open("demo.txt", "r")
- Readable will return True or False, if the opened file stream can be read. Call Readable(), if stream can be read, True will be returned.
- Read the first five characters of the file stream.
- Reading the next five characters of the file stream. Note the read does not begin at beginning again, it picks up from where it left off from last read. That is why it is important to assign to a variable, if not assigned to variable, the file must be reread.
- Return the location in file by calling tell. Note if 5 characters had been read plus and additional 5 characters above, tell would be return a location of 10.
- Reads from current position until the end of line (EOL). A number of characters can be passed to readline(70). If the number of characters passed is less the EOL, then line is read to that character. Equal to or greater than the end of line, the read stops at the EOL.
- Readlines reads the number of passed characters, or until the end of file (EOF).
- If a negative number is passed to read, readline, or readlines, file is read to EOF.
- When file tasks are done the file stream should be properly closed
Writing to a file
- Like reading a file, a file handle needs to be created, but this time passing w, as the mode, to signify write. Note if the file does not exist it will be created. If it exists, it will be truncated to zero characters (contents overwritten). See x (create mode) below to not overwrite the file.
fh = open("demo.txt", "w")
- Writable will return True if file stream can be written to.
- Write will write the text to the current position in the file. This will write quoted text to file.
fh.write("Text to write to file")
- Writing additional text, will write the text, starting at the current location. Note, no spaces or newlines(\n) unless included in the passed string. Also Note, write returns the number of characters written.
fh.write("This is some more text")
- The writelines method can be used to pass a list of lines to be written to the file.
fh.writelines(["\nThis is a line to write", "\nAnother line of text to be written"])
- If the File handle is opened using the x mode, a file will be created if it does not exist. However, if the file exists it will not be overwritten, and the write will fail.
fh = open("demo.txt", "x")
- When opening a file encoding can be specified. Most common is utf-8, which will encompass Unicode characters, and is backward compatible to ASCII.
fh = open("demounicode.txt", mode="r", encoding="utf-8")
- Using the append mode, the current file is appended to the EOF.
fh = open("demo.txt", "a")
Deleting a File
- Deleting a file requires the use of the OS module and must be imported (should be placed a top of file).
- Existence of the file should be checked prior to deleting by using
- To delete a file the remove function is used.
Wikipedia Article for UTF-8 https://en.wikipedia.org/wiki/UTF-8