尝试使用 python 复制文件时出现此错误号 2
Im getting this errno 2 when trying to copy files with python
我正在尝试从目录及其所有底层文件夹中移动大量文件,但是当我 运行 我的代码时。脚本找不到第一个 .pdf 文件。我觉得很奇怪,因为文件确实存在
import shutil
import os
#Change the working directory to where files are located
os.chdir("C:\Users\vhole\iCloudDrive\BA_Historie\")
#Get current working directory
directory = os.getcwd()
print(f"Current working directory is {directory}")
#Walking through a folder tree
for folders,subfoldes,filenames in os.walk('C:\Users\vhole\iCloudDrive\BA_Historie\'):
for filename in filenames:
if filename.endswith('.jpg') or filename.endswith('.pdf'):
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
这是我得到的回溯和错误:
Traceback (most recent call last):
File "project13.py", line 18, in <module>
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
File "C:\Users\vhole\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 248, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\vhole\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'Det_moderne_Norden_program.pdf'
Python 尝试从工作目录(即您 chdired 的目录)复制文件。因此,如果您的 pdf 文件不直接在其中,而是在其 sub-directories 之一中,则复制将失败。因此,一个解决方案是将 sub-directory 路径添加到 filename
之前。所以代替:
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
尝试:
shutil.copy(os.path.join(folders, filename),'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
我正在尝试从目录及其所有底层文件夹中移动大量文件,但是当我 运行 我的代码时。脚本找不到第一个 .pdf 文件。我觉得很奇怪,因为文件确实存在
import shutil
import os
#Change the working directory to where files are located
os.chdir("C:\Users\vhole\iCloudDrive\BA_Historie\")
#Get current working directory
directory = os.getcwd()
print(f"Current working directory is {directory}")
#Walking through a folder tree
for folders,subfoldes,filenames in os.walk('C:\Users\vhole\iCloudDrive\BA_Historie\'):
for filename in filenames:
if filename.endswith('.jpg') or filename.endswith('.pdf'):
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
这是我得到的回溯和错误:
Traceback (most recent call last):
File "project13.py", line 18, in <module>
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
File "C:\Users\vhole\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 248, in copy
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "C:\Users\vhole\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'Det_moderne_Norden_program.pdf'
Python 尝试从工作目录(即您 chdired 的目录)复制文件。因此,如果您的 pdf 文件不直接在其中,而是在其 sub-directories 之一中,则复制将失败。因此,一个解决方案是将 sub-directory 路径添加到 filename
之前。所以代替:
shutil.copy(filename,'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)
尝试:
shutil.copy(os.path.join(folders, filename),'C:\Users\vhole\iCloudDrive\BA_Historie\Samlede_tekster\', follow_symlinks = True)