Checking File Path and Manipulating Files with Python
Photo by Michal Ico on Unsplash

DataSeries highlight:

Python is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at how to check for path validity and read files with Python.

Checking Path Validity

There’re several ways to check if a path exists in Python. They’re the following:

exists — returns True is a path exists and False otherwise

— returns is a path exists and otherwise is_file — returns True is the path exists and is a file and False otherwise

— returns is the path exists and is a file and otherwise is_dir — returns True is the path exists and it’s a directory and False otherwise

For instance, we can use them as follows, given that we have the following Path object:

from pathlib import Path

path = Path('./abc/')

Then if /abc/ doesn’t exist, then running path.exists() returns False .

We can check if a path is a file by calling the is_file method as follows:

from pathlib import Path

path = Path('./foo/bar/abc.txt')

is_file = path.is_file()

Given that we created the ./foo/bar/abc.txt file, is_file should be True .

Finally, we can use the is_dir method as follows:

from pathlib import Path

path = Path('./foo/bar/')

is_dir = path.is_dir()

Then given that we created the ./foo/bar/ file, is_dir should be True .

The File Reading and Writing Process

We can use the Path object to write content to a file.

For instance, we can run the following code:

from pathlib import Path

path = Path('./foo/bar/abc.txt')

path.write_text('abc')

The code above will write abc to a file given that ./foo/bar/abc.txt exists.

We can read the content from a file into a string with the read_text method as follows:

from pathlib import Path

path = Path('./foo/bar/abc.txt')

content = path.read_text()

Then given that ‘./foo/bar/abc.txt’ has the content abc , read_text returns that and set it to content , so content ‘s value will be 'abc' .

Opening Files With the open() Function

We can use the open function to open a file. To do this, we can pass in a Path object into the open function as follows:

from pathlib import Path

file = open(Path('./foo/bar/abc.txt'), 'r')

By running the code above, we get a filehandle that we can use to read and write to the file depending on what permission we pass into the second argument.

In the example above, we opened it with read-only permission since we passed in 'r' .

Reading the Contents of Files

With the filehandle, we can call read on it to read the file.

For instance, we can write:

from pathlib import Path

content = open(Path('./foo/bar/abc.txt'), 'r').read()

to get the content of the abc.txt file. Given that we put abc in the file, then content should be 'abc' since we call read with the string.

We can read a file line by line rather than the whole file with the readlines method. This is useful for reading big files since we don’t want to read the whole file into memory.

For instance, given that our ./foo/bar/abc.txt file has the following:

abc

123

def

foo

bar

Then we can write the following code to read in the content line by line with readlines :

from pathlib import Path

file = open(Path('./foo/bar/abc.txt'), 'r')

for line in file.readlines():

print(line)

Then readlines will read the file one line at a time.

Photo by NeONBRAND on Unsplash

Writing to Files

We can use the open function to open a file and write to it.

For instance, we can use it as follows:

from pathlib import Path

with open(Path('abc.txt'), 'w+') as file:

file.write('abc')

The code above will write to the abc.txt file and create it and write to it if it doesn’t exist.

We use the with keyword to open it and then clean up automatically when writing is done.

Then abc.txt should have abc as the file content.

The code above is equivalent to:

from pathlib import Path

file = open(Path('abc.txt'), 'w+')

file.write('abc')

file.close()

Other permissions that we can pass into the 2nd argument includes 'a' for appending to a text file and 'w' for writing to an existing file.

Saving Variables with the shelve Module

We can use the shelve module to open a file and then save a variable with its value in the into a file.

For instance, we can write:

import shelve

shelf_file = shelve.open('data')

shelf_file['fruits'] = ['apple', 'orange', 'grape']

shelf_file.close()

The code above will save the value of the 'fruits’ key, which is [‘apple’, ‘orange’, ‘grape’] into a file called data .

It’s a binary file, so we have to read it back with the shelve module as follows to retrieve the data:

shelf_file = shelve.open('data')

print(shelf_file['fruits'])

Then given the data file we saved before, we’ll print out the value of the 'fruits' key, which is [‘apple’, ‘orange’, ‘grape’] .

Conclusion

We can check for file path validity with the methods in the Path object.

To open a file, we can either read in a text file with the read_text method of the Path object or the open method.

The readlines method is used to read a file line by line.

write_text is used to write text to a file. Also, we can use the write function to write to a file.
['John Au-Yeung']
['Technology', 'Software Development', 'Software Engineering', 'Programming', 'Python']