How to read/write files in Python

python-read-write-files-feature-image

In many cases, we want our data to persist after exiting program. So we will need to save it to a file. In this tutorial, we’re gonna look at way to create, save, read, write files in Python.

Related Posts:
How to copy, move, rename, delete files/folders in Python

Python File Path functions

Every file has a name and a path that indicates the location of a file on computer.

On Window, path is written using backslash (\) as the separator between folder names. Otherwise, OS X and Linux use the forward slash (/).

Create file path with correct separators

Using os.path.join() function, we can create a string for file path with the correct separators without caring about operation system.

>>> import os

>>> os.path.join('user', 'ozenero', 'python')
'user\\ozenero\\python'
# 'user/ozenero/python' on OS X or Linux

>>> os.path.join('C:\\user\\ozenero\\python', 'example.py')
'C:\\user\\ozenero\\python\\example.py'
Get Current Working Directory

Every program that is running has a current working directory, or cwd. We can get cwd as a string value with the os.getcwd() function.

>>> os.getcwd()
'C:\\Users\\TienTN\\AppData\\Local\\Programs\\Python\\Python35'

To change cwd, we use os.chdir() function.

>>> os.chdir('C:\\Windows')
>>> os.getcwd()
'C:\\Windows'

# a directory that does not exist
>>> os.chdir('C:\\Windows\\notExisted')
Traceback (most recent call last):
  File "", line 1, in 
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Windows\\notExisted'
Create new Folder

We use os.makedirs() function to create a new folder.

>>> os.makedirs('D:\\ozenero\\Tutorials\\Python')
# create 'ozenero' folder inside 'D:'
# and 'Tutorials' folder inside 'D:\ozenero'
# and 'Python' folder inside 'D:\ozenero\Tutorials'
Absolute & Relative Path

We have 2 ways to work with a file path.
– absolute path: begin with the root folder.
– relative path: relative to current working directory.

os.path module provides functions to work with absolute & relative path:
os.path.abspath(relative_path): convert relative_path into an absolute path.
os.path.isabs(path): return True if path is an absolute path and False if it is a relative path.
os.path.relpath(path, start): return a relative path from the start path to path. If we only provide path without start, start will be the current working directory.

*Note: A single dot (.) means this directory, and double dots (..) means the parent folder.

# cwd: C:\Users\TienTN\helloworld
>>> os.path.abspath('.')
'C:\\Users\\TienTN\\helloworld'

>>> os.path.abspath('.\\src')
'C:\\Users\\TienTN\\helloworld\\src'

# cwd: C:\Users\TienTN\helloworld
>>> os.path.abspath('..\\Pictures')
'C:\\Users\\TienTN\\Pictures'

# cwd: C:\Users\TienTN\helloworld
>>> os.path.abspath('..\\..')
'C:\\Users'

>>> os.path.isabs('.\\src')
False

>>> os.path.isabs('C:\\Users\\TienTN\\helloworld')
True

>>> os.path.relpath('C:\\Users', 'C:\\')
'Users'

>>> os.path.relpath('C:\\Users\\TienTN\\helloword', 'C:\\Users\\TienTN\\Pictures')
'..\\helloword'

>>> os.path.relpath('C:\\Users', 'C:\\Users\\TienTN\\helloworld')
'..\\..'

>>> os.path.relpath('C:\\Users\\Public', 'C:\\Users\\TienTN\\helloworld')
'..\\..\\Public'
Get File size

Having file path, we can find its size in bytes with os.path.getsize(path) function.

>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
27648

Having file path of a folder, we can also know the files and folders inside it with os.listdir(path) function.

>>> os.listdir('C:\\wamp64')
['alias', 'apps', 'barimage.bmp', 'bin', 'cgi-bin', 'images_off.bmp', 'images_on.bmp', 'install-english.txt', 'lang', 'license-english.txt', 'logs', 'read_after_install-english.txt', 'scripts', 'tmp', 'unins000.dat', 'unins000.exe', 'uninstall_services.bat', 'wampmanager.conf', 'wampmanager.exe', 'wampmanager.ini', 'wampmanager.tpl', 'www']
Check Path Validity

If we give a path that does not exist, functions will throw an error. So we will use some functions to check:

os.path.exists(path): return True if the path refers to the file or folder exists, return False if not.
os.path.isfile(path): return True if the path exists and it refers to a file, return False otherwise.
os.path.isdir(path): return True if the path exists and it refers to a folder, return False otherwise.

>>> os.path.exists('C:\\Windows')
True
>>> os.path.exists('C:\\notExisted')
False

>>> os.path.isdir('C:\\Windows\\System32')
True
>>> os.path.isdir('C:\\Windows\\System32\\calc.exe')
False

>>> os.path.isfile('C:\\Windows\\System32')
False
>>> os.path.isfile('C:\\Windows\\System32\\calc.exe')
True

Read/Write Files in Python

We have 3 steps:
1- Use open() function to get a File object.
2- Use File object read()/write() method.
3- Use File object close() method to close the file.

Open File

We use open(path) function with a path can be either an absolute or relative path. This function returns a File object.

mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt')

By default, we only can read data from the file, if we want to write or modify it, we can explicitly specify the mode (write mode or append mode) by passing 'w' or 'a' to open() function as the second argument.

# write mode
>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'w')

# append mode
>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'a')
Read File

Assume that the content in ozenero.txt file is:

What does ozenero mean?
Well, ozenero is derived from the words grok and konez.
- grok means understanding (something) intuitively or by empathy.
- konez expresses 'connect' that represents the idea 'connect the dots', 'connect everything'.
Read entire content

Entire content will be read as a string value with File object read() method.

>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt')
>>> ozeneroContent = mFile.read()
>>> ozeneroContent
"What does ozenero mean?\nWell, ozenero is derived from the words grok and konez.\n- grok means understanding (something) intuitively or by empathy.\n- konez expresses 'connect' that represents the idea 'connect the dots', 'connect everything'."
Read content as lines

We can get a list of string values from the file, each string for each line of text, by using File object readlines() method.

>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt')
>>> ozeneroContent = mFile.readlines()
>>> ozeneroContent
['What does ozenero mean?\n', 'Well, ozenero is derived from the words grok and konez.\n', '- grok means understanding (something) intuitively or by empathy.\n', "- konez expresses 'connect' that represents the idea 'connect the dots', 'connect everything'."]
Write to File

We have to open file in write mode or append mode, then we use File object write() method to write to a file.

If the file path passed to open() function does not exist, both modes will create a new blank file. Don’t forget to close the file with close() method to save our work.

Write mode

This mode will overwrite the content of existing file.

>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'w')
>>> mFile.write('ozenero Technology')
19
>>> mFile.write('- grok & zen')
12
>>> mFile.close()
>>> open('C:\\Users\\TienTN\\Desktop\\ozenero.txt').read()
'ozenero Technology- grok & zen'

# open with 'write mode' again
>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'w')
>>> mFile.write('Programming Tutorials')
21
>>> mFile.close()
>>> open('C:\\Users\\TienTN\\Desktop\\ozenero.txt').read()
'Programming Tutorials'
Append mode

This mode will only append text to the end of existing file.

>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'a')
>>> mFile.write('ozenero Technology')
19
>>> mFile.write('- grok & zen')
12
>>> mFile.close()
>>> open('C:\\Users\\TienTN\\Desktop\\ozenero.txt').read()
'ozenero Technology- grok & zen'

# open with 'append mode' again
>>> mFile = open('C:\\Users\\TienTN\\Desktop\\ozenero.txt', 'a')
>>> mFile.write('- Programming Tutorials')
23
>>> mFile.close()
>>> open('C:\\Users\\TienTN\\Desktop\\ozenero.txt').read()
'ozenero Technology- grok & zen- Programming Tutorials'

Save Variables to File in Python

Use shelve module

Sometimes we need our program to be able to restore data saved to the hard drive (e.g: configuration settings). The shelve module allows us to save and open variables for our program.

To use shelve module:
1- import shelve.
2- call shelve.open(path), store the returned value in shelf_variable.
3- treat shelf_variable as a dictionary and make changes if necessary.
4- call close() on shelf_variable.

For example:

>>> import shelve
>>> gkzShelf = shelve.open('ozenero')
>>> tuts = ['java', 'javascript', 'python']
>>> gkzShelf['tuts'] = tuts
>>> gkzShelf.close()

After running the code above, we will see 3 new files in cwd: ozenero.bak, ozenero.dat, ozenero.dir (ozenero.db on OS X).
Now we reopen and get data from the files:

>>> gkzShelf = shelve.open('ozenero')
>>> gkzShelf['tuts']
['java', 'javascript', 'python']

# convert to list
>>> list(gkzShelf.keys())
['tuts']
>>> list(gkzShelf.values())
[['java', 'javascript', 'python']]

>>> gkzShelf.close()
Use pprint module

The pprint module allows us to get a string that we can write to a .py file.
This file is treated as a module which we can import wherever we want to use variables stored in it.

To use pprint module:
1- import pprint
2- get string from variable using pprint.pformat() function
3- write the string to a .py file
4- import file as module and get stored values

For example:

>>> import pprint
>>> tuts = [{'title': 'Python Files', 'category': 'Python'}, {'title': 'Spring RestApis', 'category': 'Spring Framework'}]
>>> mFile = open('ozenero.py', 'w')
>>> mFile.write('tuts = ' + pprint.pformat(tuts) + '\n')
120
>>> mFile.close()

# >>> pprint.pformat(tuts)
# "[{'category': 'Python', 'title': 'Python Files'},\n {'category': 'Spring Framework', 'title': 'Spring RestApis'}]"

Now we can import ozenero module and get values easily.

>>> import ozenero
>>> ozenero.tuts
[{'title': 'Python Files', 'category': 'Python'}, {'title': 'Spring RestApis', 'category': 'Spring Framework'}]
>>> ozenero.tuts[0]
{'title': 'Python Files', 'category': 'Python'}
>>> ozenero.tuts[1]
{'title': 'Spring RestApis', 'category': 'Spring Framework'}
0 0 votes
Article Rating
Subscribe
Notify of
guest
1.7K Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments