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.

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.

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

Create new Folder

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

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.

Get File size

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

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

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.

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.

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.

Read File

Assume that the content in grokonez.txt file is:

Read entire content

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

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.

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.

Append mode

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

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:

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

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:


Now we can import grokonez module and get values easily.


By grokonez | January 11, 2019.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*