首页 > 解决方案 > 在 Python 中选择并复制多种不同的文件类型

问题描述

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

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

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

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

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

标签: pythonpython-3.x

解决方案


看看这是否有帮助。这将从源路径复制所有 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)

推荐阅读