Behaviour of os.rename()

V

venutaurus539

Hello all,
I got a suspicion on the behaviour of os.rename
(src,dst).If the src is the path of a file and dst is a new filename
this os.rename() function is infact creating a new file with the dst
name in the current working directory and leaving the src as it is. Is
this the expected behavior? If i want the actual source file in its
orignal location to be renamed without doing os.chdir() to that
directory, is that possible?

Ex: if my script ren.py contains the following code:

os.rename("C:\\Folder1\\Folder2\\file1,file2)

and my ren.py is in the folder D:\. Now if I run this
script, it is creating file2 in D:\ but I want it in C:
\Folder1\Folder2. is that possible?

When I checked the normal Windows rename function, it is
working to my expectations but I can't use it because my file is in a
deep path (>255) which Windows won't support.

Thanks in advance,
Venu.
 
T

Tim Golden

Hello all,
I got a suspicion on the behaviour of os.rename
(src,dst).If the src is the path of a file and dst is a new filename
this os.rename() function is infact creating a new file with the dst
name in the current working directory and leaving the src as it is. Is
this the expected behavior? If i want the actual source file in its
orignal location to be renamed without doing os.chdir() to that
directory, is that possible?

Ex: if my script ren.py contains the following code:

os.rename("C:\\Folder1\\Folder2\\file1,file2)

and my ren.py is in the folder D:\. Now if I run this
script, it is creating file2 in D:\ but I want it in C:
\Folder1\Folder2. is that possible?

When I checked the normal Windows rename function, it is
working to my expectations but I can't use it because my file is in a
deep path (>255) which Windows won't support.

os.rename on windows calls the Windows MoveFile API:

http://msdn.microsoft.com/en-us/library/aa365239(VS.85).aspx

Have a look at the details on that page to see what
the limitations / actions are. But remember -- as I've
indicated elsewhere -- to use the ur"\\?\c:\..." form
of the file names.

And let us know if that works :)

TJG
 
E

Emile van Sebille

Hello all,
I got a suspicion on the behaviour of os.rename
(src,dst).If the src is the path of a file and dst is a new filename
this os.rename() function is infact creating a new file with the dst
name in the current working directory and leaving the src as it is. Is
this the expected behavior? If i want the actual source file in its
orignal location to be renamed without doing os.chdir() to that
directory, is that possible?

Ex: if my script ren.py contains the following code:

os.rename("C:\\Folder1\\Folder2\\file1,file2)

os.rename("C:\\Folder1\\Folder2\\file1","C:\\Folder1\\Folder2\\file2")

Emile
 
V

venutaurus539

os.rename on windows calls the Windows MoveFile API:

 http://msdn.microsoft.com/en-us/library/aa365239(VS.85).aspx

Have a look at the details on that page to see what
the limitations / actions are. But remember -- as I've
indicated elsewhere -- to use the ur"\\?\c:\..." form
of the file names.

And let us know if that works :)

TJG

That actually was an illustration. As you've told in another chain, it
isn't working even after appending "\\?\"

Thank you,
Venu
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top