Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute path of a file object

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?

like image 797
astrofrog Avatar asked Mar 16 '10 22:03

astrofrog


People also ask

How do I find the absolute path of a path?

The function getAbsolutePath() will return the absolute (complete) path from the root directories. If the file object is created with an absolute path then getPath() and getAbsolutePath() will give the same results.

How do you create an absolute path file?

If you want an absolute path you'll need to do something like: new File("C:\\Users\\Gabrielle\\Documents\\NetBeansProjects\\CS2 Assignment 5\\src\\index. html"); You're not getting that from the request to your application because the request is URLEncoded.

What is absolute path of file in Windows?

An absolute path contains the full set of directories from the root of the file system up to your target file or directory. On Windows, an absolute path starts with a drive like C:\ . You can cd to an absolute path from anywhere on the filesystem. This is an example absolute path: C:\Users\jesstess\projects.

What is absolute path as used in file management?

An absolute path refers to the complete details needed to locate a file or folder, starting from the root element and ending with the other subdirectories. Absolute paths are used in websites and operating systems for locating files and folders. An absolute path is also known as an absolute pathname or full path.


2 Answers

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.

like image 191
bignose Avatar answered Oct 08 '22 11:10

bignose


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...)

like image 41
Glenn Maynard Avatar answered Oct 08 '22 11:10

Glenn Maynard