simple problem with os.rename() parameters - path with spaces

Peter Hansen peter at engcorp.com
Sat Sep 10 12:10:10 EDT 2005


Tom wrote:
> Drive E: is removable, so I was careful to verify that that was a factor 
> in the problem.
> 
> Yes, I can do the same renaming, with the same drive, at the command line.
> 
> I think I put the emphasis in the wrong place in my question.  This 
> isn't really about os.rename().  It is about putting a filename with 
> spaces into a string object and then using it as a parameter to an 'os' 
> command.

That can't really be all there is to the issue, since other people can 
successfully use spaces in filenames passed to 'os' commands including 
os.rename.  There must be something special with _your_ situation.  What 
else could it be except the file system?

Oh... wait, I see now.  Look closely at your error message:

Traceback (most recent call last):
   File "E:\Music\MoveMusic.py", line 64, in ?
     main();
...
   File "E:\Music\MoveMusic.py", line 49, in Visit
     os.mkdir( NewDir );
OSError: [Errno 22] Invalid argument: '"e:\\music.ogg\\Joni 
Mitchell\\ogg-8"'

Where do you think those double quotation marks came from?  What happens 
if you try the following instead of using the variables you were trying 
to use?

os.rename("e:\\music\\Joni Mitchell\\ogg-8",
     "e:\\music.ogg\\Joni Mitchell\\ogg-8")

Now try it with this and observe how you get (I predict) the same error 
message as you originally got, and note what your mistake was:

os.rename('"e:\\music\\Joni Mitchell\\ogg-8"',
     '"e:\\music.ogg\\Joni Mitchell\\ogg-8"')

(To avoid confusion, cut and paste the above lines rather than 
attempting to retype them.)

-Peter



More information about the Python-list mailing list