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

absolute path to a file

On 17/08/2019 01:07, Gregory Ewing wrote:
>> On Sat, Aug 17, 2019 at 2:27 AM Paul St George 
>> <email at paulstgeorge.com> wrote:
>>> BUT does not work with
>>> | print('test2:',os.path.realpath(n.image.filepath))|
>>> This returns only
>>> |/image01.tif|
> What does n.image.filepath look like on its own? If it starts
> with a leading slash, then os.path.realpath will think it's
> already an absolute path and leave it alone.
> The problem then is why it's getting a leading slash in the
> first place. It looks like the result of something naively
> trying to append a filename to an empty directory path.
Thank you for this. You ask what n.image.filepath looks like on its own. 
Here (below) are some tests and the results: You are right about the 
leading slashes. I have tried to remove them and then use 
os.path.realpath and os.path.abspath but it is as if the slash is still 
there. Perhaps there is a better way to remove the slash - perhaps by 
getting the name of the file. Then I could find the true path to the 
directory that contains the file, and finally join the two together.

But there must be a simpler way?

  print('Track A:',n.image.filepath)
---Track A: //image01.tif

print('Track B:',n.image.filepath[1:])
---Track B: /image01.tif

  print('Track C:',n.image.filepath[2:])
---Track C: image01.tif

  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

  print('Track G from Track C:',os.path.abspath(n.image.filepath[2:]))
---Track G from Track C: /image01.tif