Python Forum
FileNotFound when copy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FileNotFound when copy
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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'))
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020