Obtaining a full path name from file

Dave Angel davea at ieee.org
Wed May 25 07:05:16 EDT 2011


On 01/-10/-28163 02:59 PM, 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 !
>

I saw lots of responses, but I don't think anybody pointed out that the 
filename is probably invalid.  This particular string will work, but if 
you have a directory that starts with a T or an N, you may get some 
surprises.  The backslash is treated specially in a literal string.

When building a Windows directory name in a literal string, you 
generally need to do one of three things:

1) use raw literals
2) double the backslash
3) use a forward slash

DaveA





More information about the Python-list mailing list