[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

absolute path to a file

On 17Aug2019 11:51, Paul St George <email at paulstgeorge.com> wrote:
> print('Track D  from Track B:',os.path.realpath(n.image.filepath))
>---Track D  from Track B: /image01.tif
> print('Track E  from Track B:',os.path.realpath(n.image.filepath[1:]))
>---Track E  from Track B: /image01.tif
> print('Track F from Track C:',os.path.realpath(n.image.filepath[2:]))
>---Track F from Track C: /image01.tif

I know these may just be cut/paste, but you can shorten this by:

    from os.path import realpath
    print('Track F from Track C:',realpath(n.image.filepath[2:]))

I have 2 other questions:

1: Is image01.tif a real existing file when you ran this code?

realpath kind of expects that - it may do nothing for something which 
doesn't exist. I'd point out that realpath(3), which you get from the 
command "man 3 realpath", says:

    The realpath() function will resolve both absolute and relative paths and
    return the absolute pathname corresponding to file_name.  All
    components of file_name must exist when realpath() is called.

Most of the things in the os module are just thin wrappers for the OS 
supplied functions, which is why the MacOS realpath library function is 

2: What is your current working directory when you run this code?

If the underlying OS realpath fails, the Python library function might 
fall back on os.path.join(os.getcwd(), filename).

3: What is the airspeed velocity of an unladen swallow?


Cameron Simpson <cs at cskk.id.au> (formerly cs at zip.com.au)