GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing blog.feabhas.com — Python 3 File Paths - Sticky Bits - Powered by Feabhas Python 3 File PathsIf you’ve used Python for a while you will probably be familiar with the os module for working with files and directories; often called pathnames by Linux users. In moving to Python 3 you may continue to use the same os and os.path functions from Python 2.7, however a new pathlib module provides an alternative object-oriented (OO) approach. In this posting, we examine the common file handling situations; comparing the OO approach of pathlib against the procedural approach of os functions. Contents ToggleCurrent DirectoryTo obtain the current working directory with os functions we use getcwd():
The cwd variable is a standard Python string object containing the directory path:
On Windows this would be, for example:
In contrast, the OO approach provided by pathlib is to create a Path object with no arguments:
Path objects encapsulate the concept of a path (the name of a file or directory on disk) but do not necessarily imply that the path exists. A Path object can be queried and used to manipulate pathnames. Converting a Path to a string for printing returns the simplest form of the path which for the current directory is always:
To get the full, or absolute, pathname use:
which will display the same string value as returned by os.getcwd(). Directory ListingThe names in the current directory are returned by os.listdir() as a list of strings:
Normally you’d want to examine these files. In this example, we’ll simply get the file size and modification date. To start with we need to convert each name into a pathname using os.path.join():
We can now use this path variable to access the file on disk. If you’ve read the wrong tutorial you may, in the past, have written:
Which works but isn’t very Pythonic If you’re a Windows user and write:
then you have missed the part that says Python treats both forward and backward slash characters as directory separators on both Linux and Windows. In other words, it doesn’t matter which form of slash character you use – it’s all the same to Python. Of course, you may have got around the forward/backward slash issue by using the host separator character from the os module:
None of these concatenation solutions is as clean or maintainable as the os.path.join() function. Returning to the directory listing example code, we can read the file size and modification timestamp using two separate os.path function calls, e.g.:
For the pathlib.Path code example we can use iterdir() which is an iterator function, which also fits nicely with our for loop approach. As the iterator yields up Path objects there is no need to join the file name with the parent directory:
Accessing file information from a Path object is a single method call. This returns all statistics in a single object which is likely to be more efficient than the multiple os.path function calls:
The revised OO code looks thus:
Building PathnamesWe’ve already looked at how os.path.join() is used to build filenames. With Path objects there is a similar joinpath() method used to access files from a directory path. Consider where we want to access a file called ‘example.settings’ in a ‘conf’ sub-directory. Using os.path.join() we would write:
Using a Path object we now write:
Alternatively, we could use the / operator on a Path object and a string (or anther Path object):
Path objects also support wildcard filename expansion, e.g.:
The glob() method returns an iterator; which in this case we’ve used to initialise a list containing Path objects. The traditional approach for expanding wildcards is to use the glob module to get back a list of strings:
Both versions of the glob() method/function also support recursive wildcards using ‘**’ (but the glob.glob() method requires a recursive=True argument to enable this feature):
This capability means that you no longer need to use the cumbersome os.walk() method if all you want to do is find files that match a wildcard pattern. Manipulating FilesThe os module has many functions for manipulating files. The following example checks for a file called example.log in the current directory and then deletes it.
The same solution using the pathlib module
Directories can be created using the os.mkdir() function or Path.mkdir() method and empty directories removed with the rmdir() function or method. Opening FilesFinally, Python 3.6 upgraded the os and os.path functions as well as file open() to work with both strings and Path objects: this is formalised as a Path Like Object. The procedural approach for opening all Python files in the current directory looks like:
The object oriented approach looks like:
The Path class has a lot more capabilities in addition to those shown here. It is a more or less complete replacement for all of the os, os.path and glob functions and is described in the Python 3 pathlib documentation. SummaryIf you like OO languages such as C++ or Java you’ll probably prefer the Python 3 object-oriented approach using Path object. Whereas if you’re used to the procedural approach of C you may be be more comfortable using the os, os.path and glob functions. As long as you’re aware that both approaches are available with Python 3 you can make an informed decision over which one to use, rather than continue to use the procedural approach without being aware that there is an alternative. Associate Instructor at Feabhas Ltd An independent IT trainer Martin has over 40 years academic and commercial experience in open systems software engineering. He has worked with a range of technologies from real time process controllers, through compilers, to large scale parallel processing systems; and across multiple sectors including industrial systems, semi-conductor manufacturing, telecomms, banking, MoD, and government. Latest posts by Martin Bond (see all)
Like (7) Dislike (0) An independent IT trainer Martin has over 40 years academic and commercial experience in open systems software engineering. He has worked with a range of technologies from real time process controllers, through compilers, to large scale parallel processing systems; and across multiple sectors including industrial systems, semi-conductor manufacturing, telecomms, banking, MoD, and government.
About Martin BondAn independent IT trainer Martin has over 40 years academic and commercial experience in open systems software engineering. He has worked with a range of technologies from real time process controllers, through compilers, to large scale parallel processing systems; and across multiple sectors including industrial systems, semi-conductor manufacturing, telecomms, banking, MoD, and government.Leave a Reply
|
| You are visitor 01402860 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |