[Python][en] Reading and Writing Files with Python

Python File Object

A Python file object is created when a file is opened with the open() function. You can associate this file object with a variable when you open a file using the with and as keywords. You can then print the content of the file object, file_object with print(). For example:

with open('myfile.txt') as file_object:
    print(file_object)

You might see something like this on the output terminal:

<_io.TextIOWrapper name='myfile.txt' mode='r' encoding='UTF-8'>

Python Readline Method

To read only one line instead of multiple lines in a Python file, use the method .readline() on a file object that is returned from the open() function. Every subsequent .readline() will extract the next line in the file if it exists.

with open('myfile.txt') as file_object:
  print(file_object.readline())

will print only the first line in myfile.txt.

Python Readlines Method

Instead of reading the entire content of a file, you can read a single line at a time. Instead of .read() which returns a string, call .readlines() to return a list of strings, each representing an individual line in the file. Calling this code:

with open('lines.txt') as file_object:
  file_data = file_object.readlines()
print(file_data)
returns a list of strings in file_data:
# ['1. Learn Python.\n', '2. Work hard.\n', '3. Graduate.']
# Iterating over the list, file_data, and printing it:

for line in file_data:
  print(line)

Outputs:

# 1. Learn Python.

# 2. Work hard.

# 3. Graduate.

Python Read Method

After a file is opened with open() returning a file object, call the .read() method of the file object to return the entire file content as a Python string. Executing the following Python code:

with open('mystery.txt') as text_file:
  text_data = text_file.read()
print(text_data)

will produce a string containing the entire content of the read file.

Python Write To File

By default, a file when opened with open() is only for reading. A second argument 'r' is passed to it by default. To write to a file, first open the file with write permission via the 'w' argument. Then use the .write() method to write to the file. If the file already exists, all prior content will be overwritten.

with open('diary.txt','w') as diary:
  diary.write('Special events for today')

Python Append To File

Writing to an opened file with the 'w' flag overwrites all previous content in the file. To avoid this, we can append to a file instead. Use the 'a' flag as the second argument to open(). If a file doesn’t exist, it will be created for append mode.

with open('shopping.txt', 'a') as shop:
  shop.write('Tomatoes, cucumbers, celery\n')

Parsing JSON files to dictionary

JSON format is used to store key value pairs. Python’s json module allows reading such data format and parsing it to a dictionary. The json.load function takes a file object as an argument and returns the data in a dictionary format.

# Use json.load with an opened file object to read the contents into a Python dictionary.

# Contents of file.json
# { 'userId': 10 }


import json
with open('file.json') as json_file:
  python_dict = json.load(json_file)
  
print(python_dict.get('userId'))
# Prints 10

Class csv.DictWriter

In Python, the csv module implements classes to read and write tabular data in CSV format. It has a class DictWriter which operates like a regular writer but maps a dictionary onto output rows. The keys of the dictionary are column names while values are actual data.

The csv.DictWriter constructor takes two arguments. The first is the open file handler that the CSV is being written to. The second named parameter, fieldnames, is a list of field names that the CSV is going to handle.

# An example of csv.DictWriter
import csv

with open('companies.csv', 'w') as csvfile:
  fieldnames = ['name', 'type']
  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  writer.writeheader()
  writer.writerow({'name': 'Codecademy', 'type': 'Learning'})
  writer.writerow({'name': 'Google', 'type': 'Search'})

After running the above code, companies.csv will contain the following information:

name,type
Codecademy,Learning
Google,Search

Reading ZIP files

>>> import zipfile, os

>>> os.chdir('C:\\')    # move to the folder with example.zip
>>> with zipfile.ZipFile('example.zip') as example_zip:
...     print(example_zip.namelist())
...     spam_info = example_zip.getinfo('spam.txt')
...     print(spam_info.file_size)
...     print(spam_info.compress_size)
...     print('Compressed file is %sx smaller!' % (round(spam_info.file_size / spam_info.compress_size, 2)))
...
# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
# 13908
# 3828
# 'Compressed file is 3.63x smaller!'

Extracting from ZIP Files

The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.

>>> import zipfile, os

>>> os.chdir('C:\\')    # move to the folder with example.zip

>>> with zipfile.ZipFile('example.zip') as example_zip:
...     example_zip.extractall()

The extract() method for ZipFile objects will extract a single file from the ZIP file:

>>> with zipfile.ZipFile('example.zip') as example_zip:
...     print(example_zip.extract('spam.txt'))
...     print(example_zip.extract('spam.txt', 'C:\\some\\new\\folders'))
...
# 'C:\\spam.txt'
# 'C:\\some\\new\\folders\\spam.txt'

Creating and Adding to ZIP Files

>>> import zipfile

>>> with zipfile.ZipFile('new.zip', 'w') as new_zip:
...     new_zip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)

This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.