By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

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

P: n/a
Tom
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)
os.rename( "c\aa bb", "c:\cc dd" );


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"

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"'

I've tried different combinations of single backslash vs. double
backslash, and quoted vs. non-quoted, but it always fails.

The problem is not specific to os.rename. If I instead use mkdir(
SrcDir ) I get the same problem.

Thanks,
Tom.

Sep 9 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Are you sure the source directory exists and you
have rights to rename it? Because the rename works
for me.

But you may want to look at shutil.move and/or
use forward slashes (they work under Windows)

-Larry Bates
Tom wrote:
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)
os.rename( "c\aa bb", "c:\cc dd" );


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"

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"'

I've tried different combinations of single backslash vs. double
backslash, and quoted vs. non-quoted, but it always fails.

The problem is not specific to os.rename. If I instead use mkdir(
SrcDir ) I get the same problem.

Thanks,
Tom.

Sep 9 '05 #2

P: n/a
Tom wrote:
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)
> os.rename( "c\aa bb", "c:\cc dd" );
>


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"


What kind of device is drive E: ? Are you certain it allows spaces in
filenames? Can you do the same renaming *using the same drive* at the
command line or from Explorer?

-Peter
Sep 9 '05 #3

P: n/a
Tom
Peter Hansen wrote:
Tom wrote:
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)
> os.rename( "c\aa bb", "c:\cc dd" );
>


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"

What kind of device is drive E: ? Are you certain it allows spaces in
filenames? Can you do the same renaming *using the same drive* at the
command line or from Explorer?

-Peter


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.

Tom.

Sep 10 '05 #4

P: n/a
Tom
Yes, I am sure about those things.
I've tried shutil.move and got the same result.
Forward slash? I'll give that a try and report back here if it works.

Thanks,
Tom.

Larry Bates wrote:
Are you sure the source directory exists and you
have rights to rename it? Because the rename works
for me.

But you may want to look at shutil.move and/or
use forward slashes (they work under Windows)

-Larry Bates
Tom wrote:
I'm having a problem using a path with spaces as a parameter to
os.rename() in a program on WinXP.

This works fine at the command line (where the folder "c:\aa bb" exists)

os.rename( "c\aa bb", "c:\cc dd" );


But, I can't get it to work in my program, eg.

print SrcDir
print NewDir
os.rename( SrcDir, NewDir );

when I run this I get something like this:

"e:\\music\\Joni Mitchell\\ogg-8"
"e:\\music.ogg\\Joni Mitchell\\ogg-8"

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"'

I've tried different combinations of single backslash vs. double
backslash, and quoted vs. non-quoted, but it always fails.

The problem is not specific to os.rename. If I instead use mkdir(
SrcDir ) I get the same problem.

Thanks,
Tom.


Sep 10 '05 #5

P: n/a
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
Sep 10 '05 #6

P: n/a
Tom
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.
Sep 10 '05 #7

P: n/a
Tom wrote:
Peter Hansen wrote:
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


Presumably "this" means the second one, whereas for the first you got a
different message? The latter is clearly invalid, since paths can't
contain quotation marks. The former would work provided the folder
"music.ogg/Joni Mitchell" existed.
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.
If that were true, and the only problem, you would get a different
error: OSError: [Errno 2] No such file or directory
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.


Regardless of the issue with error messages, that sounds like it does
explain your problem. Great! :-)

-Peter
Sep 11 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.