Path
A file system stores information about its files. With the os.path
module in Python, we access this information. Many methods are available here.
We detect whether a file is present with path.exists
. We read dates, get sizes and list file names. We split and join paths. We can even find a common prefix of 2 paths.
Exists
This example uses the path.exists
method. We pass a string
containing a file's location on the system disk. If the file does not exist, path.exists
returns false.
IOErrors
that occur when a program tries to open a file that does not exist.from os import path # See if this file exists. # ... Write a custom message based on the result. if path.exists("/enable1.txt"): print("File exists") else: print("File not found, sorry")File exists
How many bytes are in a file? There are several ways to get this information. We could read in the file and count the bytes. But the path.getsize()
method is simpler.
path.getsize
method returns a numeric figure. This is the byte
count of the target file.from os import path # Get size of this text file. size = path.getsize("/enable1.txt") # Display the size in bytes. print(size)1916146
The path module includes a method called commonprefix. This method receives a list of paths. It scans these strings and returns the longest shared common prefix.
from os import path # Add paths to a list. paths = [] paths.append("C:\directory\name") paths.append("C:\directory\image") paths.append("C:\directory\folder\index") # Get common prefix of the paths. prefix = path.commonprefix(paths) print(prefix)C:\directory
List
filesHow can we get a list of the files in a directory? The os.listdir
method is helpful here. It receives a directory path. It returns a list of the file names in the directory.
import os # Get all files in this directory. list = os.listdir("C:\\programs\\") print(list)['file.php', 'file.pl', 'file.py', 'file.rb']
Join
This method combines paths. It receives two or more string
arguments and combines them in an "intelligent" way. It will, for example, add a slash to the end of one argument.
os.path.join
. This requires no "from" in the import directive.import os # Join directory name not including slash. result = os.path.join("C:\\programs", "file.py") print(result) # Join directory name with trailing slash. result = os.path.join("C:\\programs\\", "file.py") print(result)C:\programs\file.py C:\programs\file.py
Split
With path.split
we can separate the "folder" from the "file name." Pass a path string
to path.split
. It returns a tuple with two parts.
string
literals with an "r" prefix (unlike many of the examples on this page).from os import path example = r"C:\programs\file.txt" # Use split on path. result = path.split(example) # The result has two parts. print("Result:", result) # The second part of the tuple is the file name. print("File name:", result[1])Result: ('C:\\programs', 'file.txt') File name: file.txt
The path.getatime
, path.getmtime
and path.getctime
methods get timestamps from files. These are the last-access time, the last-modified time, and (on Windows) the creation time.
The file system provides useful information about files. The path methods in the os.path
module are helpful for retrieving this information.