RE: shutil.move has a mind of its own

Discussion in 'Python' started by Delaney, Timothy C (Timothy), Jan 11, 2005.

  1. Daniel Bickett wrote:

    > shutil.move( "C:\omg.txt" , "C:\folder\subdir" )

    ^ ^^ ^
    The problem is that backslash is the escape character. In particular,
    '\f' is a form feed.

    >>> '\o'

    '\\o'
    >>> '\f'

    '\x0c'
    >>> '\s'

    '\\s'

    Notice how for '\o' and '\s' it doubles-up the backslash - this is
    because '\o' and '\s' are not valid escapes, and so it treats the
    backslash as just a backslash. But '\f' is a valid escape.

    You have a couple of options:

    1. Use double-backslashes (to escape the backslash):
    shutil.move("C:\\omg.txt", "C:\\folder\\subdir")

    2. Use forward slashes (they work on Windows for the most part):
    shutil.move("C:/omg.txt", "C:/folder/subdir")

    3. Build your paths using os.path.join (untested):
    shutil.move(os.path.join("C:", "omg.txt"), os.path.join("C:",
    "folder", "subdir"))

    Tim Delaney
     
    Delaney, Timothy C (Timothy), Jan 11, 2005
    #1
    1. Advertising

  2. Delaney, Timothy C (Timothy)

    drs Guest

    "Delaney, Timothy C (Timothy)" <> wrote in message
    news:...
    Daniel Bickett wrote:

    > > shutil.move( "C:\omg.txt" , "C:\folder\subdir" )

    ^ ^^ ^
    > The problem is that backslash is the escape character. In particular,
    > '\f' is a form feed.


    > You have a couple of options:


    You can also include an r to make it a raw string if extra or reversed
    slashes look odd

    shutil.move( r"C:\omg.txt" , r"C:\folder\subdir" )
     
    drs, Jan 11, 2005
    #2
    1. Advertising

  3. Oh, I'm sorry, that was my mistake. The example contained that error,
    but my code does not.

    Daniel Bickett
     
    Daniel Bickett, Jan 11, 2005
    #3
  4. Delaney, Timothy C (Timothy)

    Neil Benn Guest

    Daniel Bickett wrote:

    >Oh, I'm sorry, that was my mistake. The example contained that error,
    >but my code does not.
    >
    >Daniel Bickett
    >
    >

    To be fair though - I would have expected the method to throw an error
    rather than default to cwd.

    Neil

    --

    Neil Benn
    Senior Automation Engineer
    Cenix BioScience
    BioInnovations Zentrum
    Tatzberg 47
    D-01307
    Dresden
    Germany

    Tel : +49 (0)351 4173 154
    e-mail :
    Cenix Website : http://www.cenix-bioscience.com
     
    Neil Benn, Jan 11, 2005
    #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. Greg Hurlman
    Replies:
    5
    Views:
    1,946
    Greg Hurlman
    Dec 4, 2003
  2. Chris Jackson
    Replies:
    0
    Views:
    529
    Chris Jackson
    Dec 9, 2003
  3. Saverio M.
    Replies:
    0
    Views:
    535
    Saverio M.
    Jul 3, 2006
  4. Daniel Bickett

    shutil.move has a mind of its own

    Daniel Bickett, Jan 11, 2005, in forum: Python
    Replies:
    6
    Views:
    1,561
    Steve Holden
    Jan 12, 2005
  5. Fletcher Johnson

    Why is the shutil module called shutil?

    Fletcher Johnson, Sep 24, 2011, in forum: Python
    Replies:
    11
    Views:
    891
    alex23
    Sep 26, 2011
Loading...

Share This Page