Obtaining a full path name from file
Mel
mwilson at the-wire.com
Tue May 24 12:04:40 EDT 2011
Tim Golden wrote:
> On 24/05/2011 16:36, RVince wrote:
>> s = "C:\AciiCsv\Gravity_Test_data\A.csv"
>> f = open(s,"r")
>>
>> How do I obtain the full pathname given the File, f? (which should
>> equal "C:\AciiCsv\Gravity_Test_data"). I've tried all sorts of stuff
>> and am just not finding it. Any help greatly appreciated !
>
> You're going to kick yourself:
>
> f.name
There's trouble there, though:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open ('xyzzy.txt')
>>> f.name
'xyzzy.txt'
>>> import os
>>> os.getcwd()
'/home/mwilson'
>>> os.chdir('sandbox')
>>> f.name
'xyzzy.txt'
If you open a file and don't get a full path from os.path.abspath right
away, the name in the file instance can get out-of-date.
Mel.
More information about the Python-list
mailing list