【问题标题】:Select and copy multiple different file types in Python在 Python 中选择并复制多种不同的文件类型
【发布时间】:2020-09-09 16:25:46
【问题描述】:

我想知道是否有办法在 Python 中做到这一点:

  • 从目录中选择多个特定文件(例如 .JPG 和 .PNG),复制文件并将其粘贴到另一个应用程序中。

我知道,如果我们只是将文件移动到另一个目录,我们可以执行以下操作:

for files in glob.glob(r'*PNG'):
        shutil.copy(files, dir) 

但这不允许我复制到剪贴板,所以我只能粘贴文件。我试过查看pyperclip,但这只会允许字符串。

所以基本上我尝试的是 Ctrl + A、Ctrl + C 并将其(使用pynput.keyboard)粘贴到应用程序中。但是这种方法不允许我只复制特定的文件。

【问题讨论】:

  • 看起来像是重复的帖子。你应该看看这个帖子stackoverflow.com/questions/123198/…
  • 我已经浏览过了,但是这些选项不允许将文件复制到剪贴板(就像 Ctrl+C 和 Ctrl+V 一样),对吧?

标签: python python-3.x


【解决方案1】:

看看这是否有帮助。这将从源路径复制所有 png 文件并将它们粘贴到目标路径中。为此,我们需要使用组合包。操作系统、glob、shutil。

source = "dir/path"
destination = "dir/path"

# This will target the PNG files
for images in glob.iglob(os.path.join(source, "*.png")):
    shutil.copy(images, destination)

# This will target the JPG files   
for images in glob.iglob(os.path.join(source, "*.jpg")):
    shutil.copy(images, destination)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多