Files in unix and windows

Discussion in 'Python' started by Travis James Kleeburg, Aug 20, 2004.

  1. I am trying to copy files from one directory into another using
    shutil.copy. I am having a problem getting a usable path using
    os.path.abspath because it doesnt return a string with the extra
    backslashes and i was wondering if there was a better system call to use?
    I am also looking for a system call so there wont be a problem between windows and
    unix.
     
    Travis James Kleeburg, Aug 20, 2004
    #1
    1. Advertising

  2. Travis James Kleeburg

    Jeff Epler Guest

    Which extra (back)slashes do you mean? Do you mean that os.path.abspath
    strips the trailing separator characters from its argument?

    >>> os.path.abspath("../../../../etc/")

    '/etc'
    >>> os.path.normpath("./")

    '.'

    This behavior doesn't seem to be specifically documented.

    If preserving the trailing '/' when present is important, you can easily
    get this behavior using os.path.abspath:
    def myabspath(p):
    hadsep = p and p[-1] in (os.sep, os.altsep)
    p = os.path.abspath(p)
    if hadsep: p = p + os.sep
    return p

    >>> myabspath('../../../../../etc/')

    '/etc/'
    >>> myabspath('../../../../../etc')

    '/etc'

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.5 (GNU/Linux)

    iD8DBQFBJ0ibJd01MZaTXX0RAg4mAJ9SUjjg8rDt4OY1YvVaNw8lSx9vAQCbBbk1
    E6QV8mAC602+zN7/r8j3+/s=
    =qWGo
    -----END PGP SIGNATURE-----
     
    Jeff Epler, Aug 21, 2004
    #2
    1. Advertising

  3. Travis James Kleeburg

    Byron Guest

    Hi Travis,

    You want to use forward slashes with your file names. Backslashes are
    used for special character commands, such as "/t" for <tab>, etc.

    For Windows:
    c:/aFolder/testDocument.txt

    For Unix / Linux
    /aFolder/testDocument.txt

    If you need a relative path, with no drive specified (for Windows), use:
    /aFolder/testDocument.txt

    Hope this helps,

    Byron
    ---


    Travis James Kleeburg wrote:

    > I am trying to copy files from one directory into another using
    > shutil.copy. I am having a problem getting a usable path using
    > os.path.abspath because it doesnt return a string with the extra
    > backslashes and i was wondering if there was a better system call to use?
    > I am also looking for a system call so there wont be a problem between windows and
    > unix.
    >
    >
     
    Byron, Aug 21, 2004
    #3
  4. Travis James Kleeburg

    Byron Guest

    Opps, correction. "\t" is for <tab> -- I had the slash going the wrong
    direction.

    Byron
    ---


    Byron wrote:

    > Hi Travis,
    >
    > You want to use forward slashes with your file names. Backslashes are
    > used for special character commands, such as "/t" for <tab>, etc.
    >
    > For Windows:
    > c:/aFolder/testDocument.txt
    >
    > For Unix / Linux
    > /aFolder/testDocument.txt
    >
    > If you need a relative path, with no drive specified (for Windows),
    > use:
    > /aFolder/testDocument.txt
    >
    > Hope this helps,
    >
    > Byron
    > ---
    >
    >
    > Travis James Kleeburg wrote:
    >
    >> I am trying to copy files from one directory into another using
    >> shutil.copy. I am having a problem getting a usable path using
    >> os.path.abspath because it doesnt return a string with the extra
    >> backslashes and i was wondering if there was a better system call to
    >> use? I am also looking for a system call so there wont be a problem
    >> between windows and unix.
    >>
    >>
     
    Byron, Aug 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    12
    Views:
    1,649
    Dave Thompson
    Jan 10, 2005
  2. Replies:
    18
    Views:
    625
    Dave Thompson
    Jan 10, 2005
  3. Replies:
    3
    Views:
    1,306
    Walter Roberson
    May 1, 2006
  4. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    281
    Michele Dondi
    Jan 22, 2004
  5. nntpman68
    Replies:
    3
    Views:
    102
    Jim Gibson
    Sep 12, 2008
Loading...

Share This Page