我正在制作一個 python 應用程式,其中一個功能涉及將一個目錄的內容復制到幾個不同的位置。其中一些檔案被復制到它們當前所在的同一目錄中并被重命名。它幾乎總是有效,除非它碰到這個特定的檔案。
這是進行復制的代碼。
shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))
導致崩潰的特定副本是它試圖復制/Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib到/Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.0.dylib
這是我的終端中出現的錯誤:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/<myusername>/Desktop/archive1/test_2_here/myproject/liboqs.dylib'
為什么這個檔案會拋出錯誤,而它復制的其他檔案都沒有拋出任何錯誤?
更新
根據finder,該檔案實際上是一個“別名”:

uj5u.com熱心網友回復:
也許您可以嘗試通過連接目錄路徑和檔案名來解決它,如下所示:
這里的一個問題是您沒有指定檔案的路徑。當您從父目錄執行命令時,腳本無法知道 testfile2.txt 位于輸入目錄的子目錄中。要解決此問題,請使用:
shutil.copy(os.path.join(foldername, filename), copyDirAbs)
要么
# Recursively walk the Search Directory, copying matching files
# to the Copy Directory
for foldername, subfolders, filenames in os.walk(searchDirAbs):
print('Searching files in %s...' % (foldername))
for filename in filenames:
if filename.endswith('.%s' % extension):
print('Copying ' filename)
print('Copying to ' copyDirAbs)
totalCopyPath = os.path.join(searchDirAbs, filename)
shutil.copy(totalCopyPath, copyDirAbs)
print('Done.')
Python FileNotFoundError: [Errno 2] No such file or directory 錯誤通常由 os 庫引發。此錯誤告訴您您正在嘗試訪問不存在的檔案或檔案夾。要修復此錯誤,請檢查您是否在程式中參考了正確的檔案或檔案夾。
uj5u.com熱心網友回復:
我解決這個問題的方法是讓應用程式忽略鏈接檔案。我寫了這個:
if not os.path.islink(original_path_and_file):
shutil.copy(original_path_and_file,os.path.join(redun.path, redun.file_name))
從技術上講,這更像是一種解決方法而不是修復,因為它實際上并沒有復制檔案,但我認為該應用程式在不復制鏈接檔案時仍然可以正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/432594.html
