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() 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).
import os
  • Existence of the file should be checked prior to deleting by using os.path.exists.[^1]
  • To delete a file the remove function is used.


https://www.w3schools.com/python/python_file_open.asp 1

Wikipedia Article for UTF-8 Wikipedia https://en.wikipedia.org/wiki/UTF-8 2

https://www.w3schools.com/python/ref_file_writelines.asp 3

https://www.w3schools.com/python/python_file_remove.asp 4

https://www.programiz.com/python-programming/file-operation 5

https://www.tutorialspoint.com/python/file_readlines.htm 6

  1. https://www.w3schools.com/python/python_file_open.asp

  2. https://en.wikipedia.org/wiki/UTF-8

  3. https://www.w3schools.com/python/ref_file_writelines.asp

  4. https://www.w3schools.com/python/python_file_remove.asp

  5. https://www.programiz.com/python-programming/file-operation

  6. https://www.tutorialspoint.com/python/file_readlines.htm

Up next Overview of vSphere 7 Video What’s New with Fusion and Workstation [HCP1833]
Latest posts Update macOS with an all in one alias Mac App Store Command Line Interface Update macOS with softwareupdate command Markdown Crash Course Video What’s New with Fusion and Workstation [HCP1833] File IO in Python Overview of vSphere 7 Video What is ISO? Migrating Website HTML details Tag Microsoft RD Client iOS App Create VMware ESXi USB Install Media Intel NUC Lab Hosts Hardware Setup Installing Microsoft SQL 2016, and Management Studio Deploy VMware vCenter Appliance (VCSA) Installing the First Two Domain Controllers in the VMware ESXi 6.7 Lab Environment Macchanger Utility, and Usage Install a Kali Linux VM in a VMware ESXi 6.7 Environment Install Ubuntu 18.04 Virtual Machine in a VMware ESXi 6.7 Environment Install xRDP on Ubuntu 18.04 Install Chromium on Ubuntu 18.04 Install OpenSSH on Ubuntu 18.04 Install VMware Workstation 14 on Ubuntu 18.04 Nested ESXi server Laboratorium Rattus The Animal Within Who is Veeam? RSAC OnDemand Videos Create a Windows 10 Sandbox VM on VMware Workstation 14 RSA Conference 2018 Installing VMware Workstation 14 on Windows 10 VMware vExpert 2018 Announcement, Great News