tags:

views:

105

answers:

2

This has been discussed on StackOverflow before - I am trying to find a good way to find the absolute path of a file object, but I need it to be robust to os.chdir(), so cannot use

f = file('test')
os.path.abspath(f.name)

Instead, I was wondering whether the following is a good solution - basically extending the file class so that on opening, the absolute path of the file is saved:

class File(file):

    def __init__(self, filename, *args, **kwargs):
        self.abspath = os.path.abspath(filename)
        file.__init__(self, filename, *args, **kwargs)

Then one can do

f = File('test','rb')
os.chdir('some_directory')
f.abspath # absolute path can be accessed like this

Are there any risks with doing this?

+7  A: 

One significant risk is that, once open, the process is dealing with that file by its file descriptor, not its path. On many operating systems, the file's path can be changed by any other process (by a mv operation in an unrelated process, say) and the file descriptor is still valid and refers to the same file.

I often take advantage of this by, for example, beginning a download of a large file, then realising the destination file isn't where I want it to be, and hopping to a separate shell and moving it to the right location -- while the download continues uninterrupted.

So it is a bad idea to depend on the path remaining the same for the life of the process, when there's no such guarantee given by the operating system.

bignose
A: 

It depends on what you need it for.

As long as you understand the limitations--someone might move, rename, or hard-link the file in the interim--there are plenty of appropriate uses for this. You may want to delete the file when you're done with it or if something goes wrong while writing it (eg. gcc does this when writing files):

f = File(path, "w+")
try:
    ...
except:
    try:
        os.unlink(f.abspath)
    except OSError: # nothing we can do if this fails
        pass
    raise

If you just want to be able to identify the file in user messages, there's already file.name. It's impossible to use this (reliably) for anything else, unfortunately; there's no way to distinguish between a filename "<stdin>" and sys.stdin, for example.

(You really shouldn't have to derive from a builtin class just to add attributes to it; that's just an ugly inconsistent quirk of Python...)

Glenn Maynard