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

T

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.
 
L

Larry Bates

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

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.
 
P

Peter Hansen

Tom said:
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)


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
 
T

Tom

Peter said:
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.
 
T

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 said:
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

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.
 
P

Peter Hansen

Tom said:
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
 
T

Tom

Peter said:
Tom said:
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.
 
P

Peter Hansen

Tom said:
Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top