使用shutil.copy进行备份

发布于 2025-01-30 14:55:30 字数 739 浏览 1 评论 0原文

我有一个简单的脚本,该脚本使用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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2025-02-06 14:55:30

正如错误所说 - 您一次仅通过一个目标路径 -

if not files.startswith("tableau"):
    for dest in destination:
        shutil.copy(files,destination)

即使目的地中只有一个对象它仍然是列表。
如果您知道destination列表中只有一个项目,则可以这样使用:shutil.copy(文件,目标[0])

As the error says - you should pass it only one destination path at a time -

if not files.startswith("tableau"):
    for dest in destination:
        shutil.copy(files,destination)

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])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文