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

Tom tmm at fastmail.fm
Sun Sep 11 00:37:08 CEST 2005


Peter Hansen wrote:
> 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"')

This produced the msg:
OSError: [Errno 22] Invalid argument

I'm now using forward slashes instead of backslashes - this simplifies 
things a bit.

The problem seems to be that I'm trying to create more than one 
directory at a time.  In the above example, the dir 'Joni Mitchell' 
doesn't exist.

The functions that I'm calling (os.rename and shutil.move) use mkdir, 
not makedirs.  The solution is for me to use makedirs with all of the 
path except the leaf before I move/rename the old dir.

Thanks for your help,
Tom.





More information about the Python-list mailing list