7 Tips to Play With File Names, Directory Names, and Paths in Python

Python’s OS.path module have a lots of commands/methods that are greatly useful for all things files/directories/paths. Here are seven tips for playing with files, directories, and paths using Python os.path. These commands will help you check if a given path is a directory or not? find given path is a file? check if a file exists, get size of a file, get base name of the path and get extension of a file.

How To Check If a “Name” is a Directory?

To make sure if a name is directory or not, one can use “os.path.dir” to check it. “os.path.dir(name)” will return “True”, if it is a directory and “False” if it is not.

> os.path.isdir(dir_name)
True
> os.path.isdir(file_name)
False

How To Check If a “Name” is a File?

Similarly, one can use “os.path.isfile(name)” to check if the name is a file or not. Note that “os.path.isfile” will return false, if there is no file with the name.

> os.path.isfile(file_name)
True
> os.path.isfile(dir_name)
False
# name of a file that does not exist
> os.path.isfile(file_name)
False

How To Check If a File Exists?

To check if a file exists in a directory, use “os.path.exists”.

> os.path.exists(file_name)
False
> os.path.exists(file_name)
True

How To Get File Size?

To get the size of a file, use “os.path.getsize”. It will give file size in bytes.

>os.path.getsize(file_name)
63063

How To Split Path and Get File/Directory Name?

There are numerous ways to split a path and get the directory/file name at the path and get the base name from the path.

The Python command “os.path.split(path2File)” splits the path in to head and tail, where tail is the last part in the path. Python documents says that

The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If path is empty, both head and tail are empty. Trailing slashes are stripped from head unless it is the root (one or more slashes only).

For example

> path2File = "/Users/cmdlinetips/pythonTips.pdf"
> os.path.split(path2File)
('/Users/cmdlinetips', 'pythonTips.pdf')

Another way to get the file name (or directory name) from a path is to use “os.base.basename”. Here is an example

> os.path.basename(path2File)
'pythonTips.pdf'

How To Get the Extension of a File Name from a Path?

If you just want to get the extension of a file from a path, use “os.path.splitext” and the second element from the tuple is the extension of file name.

> os.path.splitext(path2File)
('/Users/cmdlinetips/pythonTips', '.pdf')

If your path is a directory, “os.path.splitext” will have an empty string at the second element at the output tuple.

path2File = '/Users/cmdlinetips/pythonTips'
> os.path.splitext(path2File)
('/Users/cmdlinetips/pythonTips', '')

 

>os.path.dirname(path2File)
'/Users/cmdlinetips'

How to Get The Complete Path to a File in Current Directory?

The Python command “os.path.abspath” gives the full/complete path to a file in current directory.

> os.path.abspath('pythonTips.pdf')
/Users/cmdlinetips/pythonTips.pdf
> os.path.abspath("")
/Users/cmdlinetips