Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6144 > unrolled thread

Obtaining a full path name from file

Started byRVince <rvince99@gmail.com>
First post2011-05-24 08:36 -0700
Last post2011-05-25 07:05 -0400
Articles 10 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  Obtaining a full path name from file RVince <rvince99@gmail.com> - 2011-05-24 08:36 -0700
    Re: Obtaining a full path name from file Tim Golden <mail@timgolden.me.uk> - 2011-05-24 16:52 +0100
      Re: Obtaining a full path name from file RVince <rvince99@gmail.com> - 2011-05-24 09:04 -0700
        Re: Obtaining a full path name from file Chris Angelico <rosuav@gmail.com> - 2011-05-25 02:08 +1000
        Re: Obtaining a full path name from file Tim Golden <mail@timgolden.me.uk> - 2011-05-24 17:10 +0100
        Re: Obtaining a full path name from file Jean-Michel Pichavant <jeanmichel@sequans.com> - 2011-05-24 18:13 +0200
      Re: Obtaining a full path name from file Mel <mwilson@the-wire.com> - 2011-05-24 12:04 -0400
    Re: Obtaining a full path name from file Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-05-25 08:36 +0200
      Re: Obtaining a full path name from file Tim Golden <mail@timgolden.me.uk> - 2011-05-25 08:29 +0100
    Re: Obtaining a full path name from file Dave Angel <davea@ieee.org> - 2011-05-25 07:05 -0400

#6144 — Obtaining a full path name from file

FromRVince <rvince99@gmail.com>
Date2011-05-24 08:36 -0700
SubjectObtaining a full path name from file
Message-ID<6af58349-43d3-4132-ab0a-a072852e6f3a@g12g2000yqd.googlegroups.com>
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 !

[toc] | [next] | [standalone]


#6146

FromTim Golden <mail@timgolden.me.uk>
Date2011-05-24 16:52 +0100
Message-ID<mailman.2020.1306252356.9059.python-list@python.org>
In reply to#6144
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

TJG

[toc] | [prev] | [next] | [standalone]


#6147

FromRVince <rvince99@gmail.com>
Date2011-05-24 09:04 -0700
Message-ID<09bb028b-7050-4b8e-99b0-722af11918f7@y19g2000yqk.googlegroups.com>
In reply to#6146
Ha! You;re right -- but is there a way to get it without the filename
appended at the end?

On May 24, 11:52 am, Tim Golden <m...@timgolden.me.uk> 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
>
> TJG

[toc] | [prev] | [next] | [standalone]


#6149

FromChris Angelico <rosuav@gmail.com>
Date2011-05-25 02:08 +1000
Message-ID<mailman.2021.1306253299.9059.python-list@python.org>
In reply to#6147
On Wed, May 25, 2011 at 2:04 AM, RVince <rvince99@gmail.com> wrote:
> Ha! You;re right -- but is there a way to get it without the filename
> appended at the end?

Parse the file name with the os.path functions:

http://docs.python.org/library/os.path.html

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#6150

FromTim Golden <mail@timgolden.me.uk>
Date2011-05-24 17:10 +0100
Message-ID<mailman.2022.1306253449.9059.python-list@python.org>
In reply to#6147
On 24/05/2011 17:04, RVince wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#6151

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2011-05-24 18:13 +0200
Message-ID<mailman.2023.1306253603.9059.python-list@python.org>
In reply to#6147
RVince wrote:
> Ha! You;re right -- but is there a way to get it without the filename
> appended at the end?
>
> On May 24, 11:52 am, Tim Golden <m...@timgolden.me.uk> 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
>>
>> TJG
>>     
>
>   
path, fileName = os.path.split(os.path.abspath(f.name))

JM

[toc] | [prev] | [next] | [standalone]


#6148

FromMel <mwilson@the-wire.com>
Date2011-05-24 12:04 -0400
Message-ID<irgkum$rle$1@speranza.aioe.org>
In reply to#6146
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.

[toc] | [prev] | [next] | [standalone]


#6197

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-05-25 08:36 +0200
Message-ID<adkua8-mv4.ln1@satorlaser.homedns.org>
In reply to#6144
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?

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


-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [prev] | [next] | [standalone]


#6200

FromTim Golden <mail@timgolden.me.uk>
Date2011-05-25 08:29 +0100
Message-ID<mailman.2055.1306308555.9059.python-list@python.org>
In reply to#6197
On 25/05/2011 07:36, Ulrich Eckhardt wrote:
> 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?
>
> 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

[toc] | [prev] | [next] | [standalone]


#6221

FromDave Angel <davea@ieee.org>
Date2011-05-25 07:05 -0400
Message-ID<mailman.2067.1306321544.9059.python-list@python.org>
In reply to#6144
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web