shutil copyfile 在我的目标文件夹中给出 Errno 2
shutil copyfile giving Errno 2 on my destination folder
我写了如下函数:
def separate_files(filetree_list, destination):
from shutil import copyfile
from os import makedirs
from os.path import join, exists, commonprefix
try:
for file in filetree_list:
dst = join(destination,
'\'.join(file.split(commonprefix(filetree_list))[1].split('\')[0:-1]))
if not exists(dst):
print(dst, " doesn`t exist. Creating...")
makedirs(dst)
print(exists(dst), ". Path created.")
print(file)
copyfile(file, dst)
except:
print("Moving files has FAILED.")
else:
print("Moving files was SUCCESSFUL.")
当我用一个“filetree_list”列表调用它时,该列表包含单个元素和用 os.path.join 准备的目标:
filetree_list = ['C:\Users\<username>\staging_folder\folder_1\filename.xlsx']
destination = os.path.join(staging_dir, 'Sorted', 'Unwanted_files')
我收到以下错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-29-c3525a60c859> in <module>()
14 print(exists(dst), ". Path created.")
15 print(file)
---> 16 copyfile(file, dst)
C:\ProgramData\Anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
119 else:
120 with open(src, 'rb') as fsrc:
--> 121 with open(dst, 'wb') as fdst:
122 copyfileobj(fsrc, fdst)
123 return dst
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\<username>\staging_folder\Sorted\Unwanted_files\'
奇怪的是(此刻让我发疯),这个文件夹确实存在,并且是由 separate_files 中的“if not exists(dst)”块创建的功能 - 我什至可以在 Windows 资源管理器中看到它!
雪上加霜的是,当我 运行 这个函数使用相同的目标参数但列表中有多个字符串时,它执行了我期望的操作并且没有错误!
任何人都可以深入了解我在这里做错了什么吗?
https://docs.python.org/3/library/shutil.html#shutil.copyfile:
shutil.<b>copyfile</b>(<i>src</i>, <i>dst</i>, <i>*</i>, <i>follow_symlinks=True</i>)
Copy the contents (no metadata) of the file named src
to a file named dst
and return dst
. src
and dst
are path names given as strings. dst
must be the complete target file name; look at shutil.copy()
for a copy that accepts a target directory path.
您正在尝试将目录作为 dst
传递给 copyfile
。那是行不通的。
我写了如下函数:
def separate_files(filetree_list, destination):
from shutil import copyfile
from os import makedirs
from os.path import join, exists, commonprefix
try:
for file in filetree_list:
dst = join(destination,
'\'.join(file.split(commonprefix(filetree_list))[1].split('\')[0:-1]))
if not exists(dst):
print(dst, " doesn`t exist. Creating...")
makedirs(dst)
print(exists(dst), ". Path created.")
print(file)
copyfile(file, dst)
except:
print("Moving files has FAILED.")
else:
print("Moving files was SUCCESSFUL.")
当我用一个“filetree_list”列表调用它时,该列表包含单个元素和用 os.path.join 准备的目标:
filetree_list = ['C:\Users\<username>\staging_folder\folder_1\filename.xlsx']
destination = os.path.join(staging_dir, 'Sorted', 'Unwanted_files')
我收到以下错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-29-c3525a60c859> in <module>()
14 print(exists(dst), ". Path created.")
15 print(file)
---> 16 copyfile(file, dst)
C:\ProgramData\Anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
119 else:
120 with open(src, 'rb') as fsrc:
--> 121 with open(dst, 'wb') as fdst:
122 copyfileobj(fsrc, fdst)
123 return dst
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\<username>\staging_folder\Sorted\Unwanted_files\'
奇怪的是(此刻让我发疯),这个文件夹确实存在,并且是由 separate_files 中的“if not exists(dst)”块创建的功能 - 我什至可以在 Windows 资源管理器中看到它!
雪上加霜的是,当我 运行 这个函数使用相同的目标参数但列表中有多个字符串时,它执行了我期望的操作并且没有错误!
任何人都可以深入了解我在这里做错了什么吗?
https://docs.python.org/3/library/shutil.html#shutil.copyfile:
shutil.<b>copyfile</b>(<i>src</i>, <i>dst</i>, <i>*</i>, <i>follow_symlinks=True</i>)
Copy the contents (no metadata) of the file named
src
to a file nameddst
and returndst
.src
anddst
are path names given as strings.dst
must be the complete target file name; look atshutil.copy()
for a copy that accepts a target directory path.
您正在尝试将目录作为 dst
传递给 copyfile
。那是行不通的。