Python File Operations

02/28/2024

Table of Contents

Click to expand
  1. Opening a file
  2. Closing a file
  3. Reading a file
  4. Writing to a file

Opening a file

File operations usually start with creating a file object.

f = open(fileName, mode)
Mode Description
r Opens a file for reading. (default)
w Opens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
x Opens a file for exclusive creation. If the file already exists, the operation fails.
a Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.
t Opens in text mode. (default)
b Opens in binary mode.
+ Opens a file for updating (reading and writing)

Examples

f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode

Closing a file

When you are done using the file, you should close it via:

f.close()

You can also use python's with function so you don't need to close the file:

with open('test.txt', 'w') as f:
  # file operations

Reading a file

To read the contents of a file line by line by using a for loop:

with open('test.txt', 'w') as f:
  for line in f:
    print(line)

Or you can use the readLine() file method:

print(f.readline())       # "This is my first file\n"
print(f.readline())       # "This file\n"
print(f.readline())       # "contains three lines\n"

Writing to a file

To write to a file, use the write() method.

f.write("this is line one \n")
f.write("this is line two \n")
f.write("this is line three \n")

© 2024 by Ryan Rickgauer