0

My python script is on partition D and I want to rename files in folders on partition E:

I can't move the folder/files to D, because of limited space.

path= "E:\\A" 

for f in os.listdir(path):

    f_name, f_ext = os.path.splitext(f)

    empty, f_number, f_title = (f_name.split('_'))
    f_title = f_title.strip()

    if len(f_number) == 2:
        f_number = '0' + f_number

   f_filename = '_{}_{}{}'.format(f_number, f_title, f_ext)
   file = os.path.join(path, f)
   os.rename(file, f_filename)

I get the error:

[WinError 17] The system cannot move the file to a different disk drive:

I found an answer here to use shutils. So I tried:

 src = os.path.join(path, f)  
 path2 = "E:\\A2"   
 dst = os.path.join(path2, f_filename)    
 shutil.move(src, dst)

[Errno 2] No such file or directory: "E:\A2\filename"

That's strange because I'm taking the file from A and move it to A2 with a new filename, so clearly doesn't exist on A2.

user3541631
  • 3,686
  • 8
  • 48
  • 115
  • What are the values of `file` and `f_filename` showing as before you call `os.rename(file, f_filename)` ? – Jon Clements Sep 15 '18 at 07:58
  • file is the original name of the file and f_file_name is the new name; If I'm on the same partition is working properlly; – user3541631 Sep 15 '18 at 08:01
  • 2
    Well yes.... but your error would indicate you weren't issuing the rename specifying the E drive... that's why I was asking you to check what the values are for those... there's nothing stopping a rename command that equates to `rename E:\test.txt E:\test2.txt`... (even if you're on C or D or whatever) – Jon Clements Sep 15 '18 at 08:03
  • you are correct, I attached the path to initial file, but not to the f_filename, considering the the path is already known, and only filename is changed – user3541631 Sep 15 '18 at 09:15

1 Answers1

0

Judging from the error message, I suspect the problem is that you are attempting to move those files into the "A2" directory before that directory is created. If A2 doesn't exist yet, you can create it using os.mkdir.

See this question for more details about that.

Souperman
  • 5,057
  • 1
  • 14
  • 39