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

Search Discussions

  • Tim Golden at May 24, 2011 at 3:52 pm

    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
  • RVince at May 24, 2011 at 4:04 pm
    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 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
  • Chris Angelico at May 24, 2011 at 4:08 pm

    On Wed, May 25, 2011 at 2:04 AM, RVince 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
  • Tim Golden at May 24, 2011 at 4:10 pm

    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
  • Jean-Michel Pichavant at May 24, 2011 at 4:13 pm

    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 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
  • Mel at May 24, 2011 at 4:04 pm

    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.
  • Michael Kent at May 24, 2011 at 7:13 pm
    If a filename does not contain a path component, os.path.abspath will prepend the current directory path onto it.
  • Ulrich Eckhardt at May 25, 2011 at 6:36 am

    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
  • Tim Golden at May 25, 2011 at 7:29 am

    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
  • Dave Angel at May 25, 2011 at 11:05 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 24, '11 at 3:36p
activeMay 25, '11 at 11:05a
posts11
users8
websitepython.org

People

Translate

site design / logo © 2021 Grokbase