使用shutil.copy进行备份
我有一个简单的脚本,该脚本使用shutil
python模块复制遵循特定模式的文件(而不是startwith wast tableau'字符串),然后从源到目标,但我收到了错误如图所示。在图像中(typeError:stat:路径应为字符串,字节,os.Pathike或Integer,而不是列表)。
# Importing required packages
import shutil
import os
# Setting variables
source=os.listdir(r"C:\Users\{username}\Documents\Workbooks")
destination=os.listdir(r"G:\My Drive\Tableau Workbooks")
# Looping into the files
for files in source:
if not files.startswith("tableau"):
shutil.copy(files,destination)
包含源文件的路径显然是文件列表,那么为什么我应该将其设置为shaltil参数以从源到目标复制这些文件?
I have a simple script that uses shutil
python module to copy files that follow a specific pattern (not startwith "tableau" string), and then move from source to destination, but I am getting the error as shown in the image bellow (TypeError: stat: path should be string, bytes, os.PathLike or integer, not list).
# Importing required packages
import shutil
import os
# Setting variables
source=os.listdir(r"C:\Users\{username}\Documents\Workbooks")
destination=os.listdir(r"G:\My Drive\Tableau Workbooks")
# Looping into the files
for files in source:
if not files.startswith("tableau"):
shutil.copy(files,destination)
The path that contains the source files is obviously a list of files, so why should I set as shutil parameter to copy those files from source to destination?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误所说 - 您一次仅通过一个目标路径 -
即使
目的地中只有一个对象
它仍然是列表。如果您知道
destination
列表中只有一个项目,则可以这样使用:shutil.copy(文件,目标[0])
As the error says - you should pass it only one destination path at a time -
Even if there is only one object in
destinations
it is still a list.If you know that you have only one item in the
destination
list, you can use it like this:shutil.copy(files, destination[0])