shutil.move has a mind of its own

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Tue Jan 11 00:08:17 EST 2005


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



More information about the Python-list mailing list