473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
7 9355
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fernando Armenta | last post by:
Hello! I am new to Python and I would like to write the following code: I need to check for a directory and move it to other directory if is found. The directory always starts with...
16
by: dudufigueiredo | last post by:
I have one folder containing mp3 files, the folder is: C:\My Shared Folder\Rubber Soul And the files are: 01 drive my car.mp3 02 norwegian wood.mp3 03 you won't see me.mp3 04 nowhere man.mp3...
2
by: Shaker | last post by:
Hello, I would like to write a program that would automatically take out the spaces in directory names and file names . I would like this because my web server does not allow spaces in names. I...
5
by: Stu Carter | last post by:
Hi, ENV: Windows 2003 Server SP1 (+all updates), VS 2003, .Net 1.1 SP1 We've got an ASP.Net web application using State Service. All is fine until we tried to use the app through a virtual...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
3
by: Mr Utkal Ranjan | last post by:
Hi Friends I want to launch notepad with a specific file on a command click event. So I was using the "Shell" function from VB on a command click event.For ex: Shell "Notepad.exe C:\Program...
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
8
by: yguan08 | last post by:
I have trouble of obtaining the file size of a file because the fullpath exceeds 255 characters. I get this message with os.path.getsize(fullpath). fullpath = r"\\LOSSSFS002\NWE_TECHNICAL\05....
9
by: Keith G Hicks | last post by:
I'm having a lot of trouble with "file in use" errors in my "folder watcher" project. Starting and stopping the watcher and reading my XML file work fine. Once the watcher is started, I'm reading...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.