Absolute path of a file object
file, filesystems, python
Solution
One significant risk is that, once the file is 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 some 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.
Problem
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?