Python脚本,用于迭代文件目录并附加与模式匹配的文件

2024-03-29 08:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我的windows目录中有一些csv文件,它们以月份分隔,例如:

C:\201801\customerlist_201801;
C:\201802\customerlist_201802;
...
..
.
C:\201810\customerlist_201810;

并非所有目录都有一个文件。我需要一个python脚本来遍历目录(从用户提供的month:201801,然后每次递增1),拾取文件,并继续追加以生成一个文件,该文件是所有文件的串联

你能告诉我从哪里开始吗? 使用Windows编程(即cmd行脚本)是否更容易? 所有文件夹都有其他子文件夹和文件


Tags: 文件csv用户目录脚本文件夹cmdwindows
1条回答
网友
1楼 · 发布于 2024-03-29 08:07:54

这里有一个基本的解决方案

files = ["C:\\%d\\customerlist_%d"%(i,i) for i in range(201801,201810)]
for file in files:
    with open(file) as csvfile:
        info = csv.reader(csvfile, delimiter=',')
        info_types = []
        records = 0
        for row in info:
            #Here you can process them as you like i.e append the content

相关问题 更多 >