根据文件夹的条件导入CSV文件,该文件夹中的另一个文件夹中
我想根据另一个文件夹中文件夹的条件复制和粘贴CSV文件。
主文件夹的名称是“ main”,其中有文件夹,即main_1,main_2,main_3,......和main_45。每个文件夹中也有CSV文件,即A1,B2,C3,D4,E5,........................................................................................................................................................................
我只想从文件夹中复制A1.CSV,B2.CSV和C3.CSV,然后将它们粘贴到另一个空文件夹中。对我来说,它看起来太复杂了,但我敢肯定,对于专家来说,这很容易。提前致谢
I would like to copy and paste csv files based on conditions from folders inside another folder.
The main folder's name is “main”, and there are folders inside it, namely main_1, main_2, main_3,......, and main_45. There are also csv files inside each folder, namely, a1, b2, c3, d4, e5,........z30.
I just want to copy a1.csv, b2.csv, and c3.csv from the folders and paste them into another empty folder. It looks too complex to me, but I am sure it would be easy for experts. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OS.Walk
通过目录进行迭代,并在字符串中显示所有文件。因此,通过
MAIN
文件夹进行迭代,如果文件名以.csv
结束,则使用shutil.copyfile.copyfile
函数docs:
https://docs.python.org/3/library/os.html
=“ https://docs.python.org/3/library/shutil.html” rel =“ nofollow noreferrer”> https://docs.python.org/3/library/library/shutil/shutil/shutil.html
os.walk
iterates through directory and show all files in string.So iterate through
main
folder and if the file name ends with.csv
, then copy it withshutil.copyfile
functiondocs:
https://docs.python.org/3/library/os.html
https://docs.python.org/3/library/shutil.html
如果您希望在某个文件夹和子文件夹下所有CSV文件名,那么一种简单的方法是使用python的
glob.glob()
函数。有了此,您可以为CSV文件指定*。csv
,也可以使其重复到子文件夹中:如果您只想复制启动
a1
A3 或
C10
例如,正则表达式可以有所帮助:If you want all CSV filenames under a certain folder and sub-folders, an easy approach would be to use Python's
glob.glob()
function. With this you can specify*.csv
for just the CSV files and also make it recurse into sub-folders:If you only want to copy CSV filenames which start
a1
a3
orc10
for example, a regular expression could help: