Search
Bits of advice about working with filesystem using Python.
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
'filepath\\filename'
'filepath/filename'
We could also use the os.path.join() function. Thus
>>> os.path.join(fpath, fname)
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')
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__) )
Completely universal way to do this uses inspect module.
import inspect fpath = os.path.dirname( inspect.getfile( inspect.currentframe() ) )