Warning
This page is located in archive.

File System

Bits of advice about working with filesystem using Python.

Working with directories

It is quite likely that you will want to either use data from a file or to write data into a file. The filename will usually be in one variable (i.e. fname) and the path in another (i.e. fpath). To access the file, you have to join those with a correct separator. For Unix/Linux it is /, for Windows it is \, which, however, has to written as \\. (Because \ is used to write special characters)

You do not have to worry about OS detection, just use standard module os. I.E. the os.sep variable always contains the right separator for the case. So, the following code

>>> import os
>>> fpath = 'filepath'
>>> fname = 'filename'
>>> fpath + os.sep + fname
will have this output under Windows
'filepath\\filename'
and this output under Linux
'filepath/filename'

We could also use the os.path.join() function. Thus

>>> os.path.join(fpath, fname)
will give same output as the above examples using os.sep. Another advantage of using os.path.join() is also that it deals with cases when fpath already contains a separator.

Saving file to current directory

You will also want your code to be able to save some data for later use. The most logical place for this is the same directory your program is in (or its subdirectory). You will probably think of using relative path, which derives the file location from the current directory. So, if you run your script script.py which contains

f = open('data.txt','w')
the new file file, data.txt will be created in the same directory script.py is in.

Problems begin, when your script script.py is imported in other modules and those try to use it, because in this case, current directory is the directory the main script is run from.

If you want to always save the new file relative to the imported module, you can use

fpath = os.path.dirname( os.path.abspath(__file__) )
However, this construction can fail, if we do not import the module, but run it, i.e. using execfile(), because in this case the file variable does not get set.

Completely universal way to do this uses inspect module.

import inspect
fpath = os.path.dirname( inspect.getfile( inspect.currentframe() ) )

courses/ae4b99rph/tutorials/python/filesystem.txt ยท Last modified: 2013/10/04 13:02 (external edit)