python shutil复制中文文件decodeerror_shutil copyfile&[错误2]

我写了一个函数如下: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操作系统公司名称:

^{pr2}$

我得到以下错误:---------------------------------------------------------------------------

FileNotFoundError Traceback (most recent call last)

in ()

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\\\\staging_folder\\Sorted\\Unwanted_files\\'

奇怪的是,这个文件夹确实存在,而且是由“如果不存在(dst)”块在单独的文件功能中创建的,我甚至可以在Windows资源管理器中看到它!在

雪上加霜的是,当我用相同的目的地参数运行这个函数,但是在一个列表中有多个字符串时,它做了我期望的事情,没有错误!在

有人能提供我在这里做错了什么吗?在

更新:

更新了上面的代码以反映IDE中的函数。已更正print(file)和copyfile(file,dst)旁边的缩进错误,以将其包含在循环下。

copyfile()的dst参数也更新为“dst”,而不是join(destination,文件.split(commonprefix(filetree_list))[1])。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值