There are a few things that are a bit annoying in the os module inside the Python standard library. For me, one of the biggest bugbears is the os.path module, and path handling within this. Python, as an object oriented language, has this really cool idea that everything is an object; even class definition and functions are objects. So how come the path module is different?
Well, path is an object as well, but it's a string object, not a path object. Since it is really easy to observe that files and directories have something to say about themselves by design, and that what they say is more than a string object can carry, I decided to write a simple alternative.
Python is often compared with Ruby, so let's have a look at how things are done there in Pathname. Whether or not you are a Ruby fan, I think we can say that the Pathname API is pretty good. Following a few ideas borrowed Ruby, I decided to propose something on this subject and create my own path implementation. As a result, I created a Python package and called it os.path2, which would be the new version of os.path with a ‘more’ object oriented approach to path handling in Python.
Below are a few examples of what is possible with the package.
>>> from os import path2 >>> path2('/var/log') /var/log >>> path2('/var', 'log') /var/log >>> path2('/var/log/syslog').a_time 1358549788.7512302 >>> path2('/home/you/file').user 'you' >>> path2('.').mod '0775' >>> [(element.user, element.group, element.mod) for element in path2('.')] [('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0664'), ('user', 'user', '0775'), ('user', 'user', '0664')]
The path2 object is also an instance of basestring, so all methods implemented for string/unicode will work here as well.
>>> path2('/home/user/Projects/os.path2').split('/') ['', 'home', 'user', 'Projects', 'os.path2'] >>> path2('/home/user/test_tmp_directory').replace('_', '-') '/home/user/test-tmp-directory' >>> location = path2('/home/user/test_tmp_directory') >>> location.mv(location.replace('_', '-'))
If you are interested, give it a go. The full API description if available here. The code is on github. The package is available in the usual way, from PyPi.
$ pip install os.path2
This idea is not new. Even though I really like my implementation, I need to be honest here and suggest a few other path projects that were created a bit earlier than mine and have broadly similar approaches to solving the path problem:
And there are actually two PEPs about it as well: