Obtaining a full path name from file

R

RVince

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 !
 
T

Tim Golden

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

TJG
 
R

RVince

Ha! You;re right -- but is there a way to get it without the filename
appended at the end?
 
M

Mel

Tim said:
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.'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.
 
T

Tim Golden

Ha! You;re right -- but is there a way to get it without the filename
appended at the end?

Well, just use the functions in os.path, specifically os.path.dirname...

TJG
 
J

Jean-Michel Pichavant

RVince said:
Ha! You;re right -- but is there a way to get it without the filename
appended at the end?
path, fileName = os.path.split(os.path.abspath(f.name))

JM
 
U

Ulrich Eckhardt

RVince said:
s = "C:\AciiCsv\Gravity_Test_data\A.csv"
f = open(s,"r")

How do I obtain the full pathname given the File, f?

Apart from the issue that the 'name' attribute is only the name used to open
the file, there is another issue, though not on the platform you're using:
Multiple directory entries can point to the same file, all of which can be
changed (including deletion!) even while you have the file open.

I'm not sure what problem you're trying to solve, but I'm afraid your
approach is at least limited.

Good luck!

Uli
 
T

Tim Golden

Apart from the issue that the 'name' attribute is only the name used to open
the file, there is another issue, though not on the platform you're using:
Multiple directory entries can point to the same file, all of which can be
changed (including deletion!) even while you have the file open.

FWIW that's true even on Windows. (Although arguably less common).
I'm not sure what problem you're trying to solve, but I'm afraid your
approach is at least limited.

Depends on what the requirement is. If it is, essentially: "give me
at least one of the names this file had when I opened it", then this
approach is surely adequate. Certainly, things could have happened in
the meantime. Obviously, only the OP can know the circumstances he's
dealing with, but ISTM that far and away the most common case will
be that the file has exactly one name and that it hasn't changed.

TJG
 
D

Dave Angel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top