0

I am trying to look for 'jpg,jpeg,png' files in my USB drive and trying to move them one by one to a new folder. when i try to move it manually one file, it works but when i run the following program it fails. Please let me know whats the problem here.

import re
import os
import ntpath as path
import shutil

path="E:\\Mac"

newpath="E\\Mac\\MovedPics"


os.chdir(path)
expr=r'\.(jpg|JPG|jpeg|JPEG|png|PNG)$'

for file in os.listdir(path):

    if os.path.isfile(file):
       match=re.search(expr,file)
       if match:
          abspath=os.path.abspath(file)
          print('REGEXP MATCHED :-',abspath)
          move=shutil.move(abspath,newpath)
          if move:
             print('MOVE SUCCESSFUL :-',file)
          else:
             print('MOVE FAILED:-',file)
             break
    else:
       print('DESTINATION DIR ',newpath, ' DOESNT EXIST', file,':', os.getcwd())

Error:-

DESTINATION DIR E\Mac\MovedPics DOESNT EXIST voice_instructions_imperial 2.zip : E:\Mac DESTINATION DIR E\Mac\MovedPics DOESNT EXIST usbpicsdata.txt : E:\Mac REGEXP MATCHED :- E:\Mac\tattoo4.jpg Traceback (most recent call last): File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 544, in move os.rename(src, real_dst) FileNotFoundError: [WinError 3] The system cannot find the path specified: 'E:\Mac\tattoo4.jpg' -> 'E\Mac\MovedPics'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 7, in move=shutil.move(abspath,newpath) File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 558, in move copy_function(src, real_dst) File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 257, in copy2 copyfile(src, dst, follow_symlinks=follow_symlinks) File "C:\Users\aryan\AppData\Local\Programs\Python\Python36-32\lib\shutil.py", line 121, in copyfile with open(dst, 'wb') as fdst: FileNotFoundError: [Errno 2] No such file or directory: 'E\Mac\MovedPics'

1
  • when i try to move that file manually, it works. >>shutil.move('E:\\Mac\\tattoo4.jpg','E:\\Mac\\MovedPics') 'E:\\Mac\\MovedPics\\tattoo4.jpg' >>> Commented Jun 30, 2017 at 7:37

1 Answer 1

1

There's a missing : in newpath:

newpath="E:\\Mac\\MovedPics"
          ^

BTW, you can use raw string literal (you can avoid escaping backslashes):

newpath = r"E:\Mac\MovedPics"  # == "E:\\Mac\\MovedPics"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Cant believe i missed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.