python通过通配符复制文件 [英] python copy files by wildcards

查看:100
本文介绍了python通过通配符复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习python(python 3),可以通过执行以下操作将1个文件复制到新目录

I am learning python (python 3) and I can copy 1 file to a new directory by doing this

import shutil 
shutil.copyfile('C:/test/test.txt', 'C:/lol/test.txt')

我现在想要做的是将所有* .txt文件从C:/复制到C:/ test

What I am now trying to do is to copy all *.txt files from C:/ to C:/test

*。txt是通配符,用于搜索硬盘上的所有文本文件

*.txt is a wildcard to search for all the text files on my hard drive

推荐答案

import glob
import shutil
dest_dir = "C:/test"
for file in glob.glob(r'C:/*.txt'):
    print(file)
    shutil.copy(file, dest_dir)

这篇关于python通过通配符复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆