python - 在 Python 中选择并复制多种不同的文件类型
问题描述
我想知道是否有办法在 Python 中做到这一点:
- 从目录中选择多个特定文件(例如 .JPG 和 .PNG),复制文件并将其粘贴到另一个应用程序中。
我知道,如果我们只是将文件移动到另一个目录,我们可以执行以下操作:
for files in glob.glob(r'*PNG'):
shutil.copy(files, dir)
但这不允许我复制到剪贴板,所以我可以粘贴文件。我试过看,pyperclip
但这只会允许字符串。
所以基本上我尝试的是 Ctrl + A、Ctrl + C 并将其(使用pynput.keyboard
)粘贴到应用程序中。但是这种方法不允许我只复制特定文件。
解决方案
看看这是否有帮助。这将从源路径复制所有 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)
推荐阅读
- reactjs - 如何处理 OPTIONS 请求中的自定义标头
- android - Android 上的 TextureView 转换 - 缩放和裁剪问题
- hive - 映射器数量:Mapreduce 与 Sqoop
- python - 难以理解@property 如何知道哪个变量是属性?
- sql - Hive - 在字符串/行中的任意位置找到 2 个字符 - RLIKE
- android - GlobalScope.launch(Main) 中的同步函数:Kotlin
- java - Android seekbar 值更改按钮
- php - 如何使用 megic 方法访问 php 中未定义的静态变量?
- git - Gitlab-Runner diff 命令不适用于文件的俄语名称
- cron - Python - 如何每天凌晨 2 点运行 cronjob