Python Forum
FileNotFound when copy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: FileNotFound when copy (/thread-19579.html)



FileNotFound when copy - linh_py - Jul-05-2019

hi everybody, please help me
i have function, it find the file and copy to new folder.
def BKZ(folder):
    print("Searching.............")
    listFiles = []
    for dirpath,dirfolder,files in os.walk(folder):
        for f in files:
            if f.endswith(".pdf") or f.endswith(".docx") or f.endswith(".doc"):
                listFiles.append(os.path.join(dirpath,f))

    # create temp folder
    serverPath = os.path.join(os.getcwd(),"zip")
    os.makedirs(serverPath,exist_ok=True)
    for address in listFiles:
        # copy all searched file to temp folder
        shutil.copy(address,serverPath)
    print("Done!")
    
if i create folder zip in my project folder and i create folder zip in different folder, then give different results? each folder zip give FileNotFoundError: No such file or directory with different files
i don't understand Sad


RE: FileNotFound when copy - Gribouillis - Jul-05-2019

It would be a good idea to post the complete error traceback that python prints in the console as well as the whole script because the error is probably in the part that is not shown above.


RE: FileNotFound when copy - linh_py - Jul-05-2019

1. If zip file in my project folder:
Traceback (most recent call last):
  File "C:/Users/Quang Linh/PycharmProjects/AutomateTheBoringStuff/Organizing Files/[project] Backup Folder to ZIP/pdf and doc.py", line 35, in <module>
    BKZ("E:\\")
  File "C:/Users/Quang Linh/PycharmProjects/AutomateTheBoringStuff/Organizing Files/[project] Backup Folder to ZIP/pdf and doc.py", line 24, in BKZ
    shutil.copy(address,serverPath)
  File "C:\Users\Quang Linh\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 245, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\Quang Linh\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 121, in copyfile
    with open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Quang Linh\\PycharmProjects\\AutomateTheBoringStuff\\Organizing Files\\[project] Backup Folder to ZIP\\zip\\TT 46.2015. BGTVT quy định tải trọng, khổ giới hạn ĐB; lưu hành xe quá tải trọng, xe quá khổ giới hạn, xe bánh xích trên đường bộ; vận chuyển hàng siêu trường, siêu trọng; giới hạn xếp hàng hóa trên PT khi TGGT ĐB.doc'
=> error from desnation path
2. If zip file in different folder, ex D:\\
Traceback (most recent call last):
  File "C:/Users/Quang Linh/PycharmProjects/AutomateTheBoringStuff/Organizing Files/[project] Backup Folder to ZIP/pdf and doc.py", line 35, in <module>
    BKZ("E:\\")
  File "C:/Users/Quang Linh/PycharmProjects/AutomateTheBoringStuff/Organizing Files/[project] Backup Folder to ZIP/pdf and doc.py", line 24, in BKZ
    shutil.copy(address,serverPath)
  File "C:\Users\Quang Linh\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 245, in copy
    copyfile(src, dst, follow_symlinks=follow_symlinks)
  File "C:\Users\Quang Linh\AppData\Local\Programs\Python\Python37-32\lib\shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\NAG\\DESIGN LIBRARY\\LR\\Bản sao của aphoto.vn - Preset Lightroom và camera tổng hợp\\aphoto.vn - Preset Lightroom và camera tổng hợp\\Preset Lightroom\\55 action chinh mau dep\\dep da\\Portrait Enhancing Photoshop Action - VietDesigner.net\\MediaLoot_License_Agreement.pdf'
=> error from source path


RE: FileNotFound when copy - perfringo - Jul-05-2019

Not to address the problem but suggestion to improve code:

if f.endswith(".pdf") or f.endswith(".docx") or f.endswith(".doc"):
str.endswith() accepts tuple as argument:

Quote:Return True if the string ends with the specified suffix, otherwise return False. suffix can also be a tuple of suffixes to look for.

so one can use:

if f.endswith(('.pdf', '.docx', '.doc'))