[英]Python: How do I iterate over several files with similar names (the variation in each name is the date)?
I wrote a program that filters files containing to pull location and time from specific ones. 我编写了一个程序,用于过滤包含从特定位置提取位置和时间的文件。 Each file contains one day's worth of tweets. 每个文件包含一天的推文。
I would like to run this program over one year's worth of tweets, which would involve iterating over 365 folders with names like this: 2011- - .tweets.dat.gz, with the stars representing numbers that complete the file name to make it a date for each day in the year. 我希望运行这个程序超过一年的推文,这将涉及迭代365个文件夹,其名称如下:2011- - .tweets.dat.gz,星号代表完成文件名的数字,使其成为一个这一年中每一天的日期。
Basically, I'm looking for code that will loop over 2011-01-01.tweets.dat.gz, 2011-01-02.tweets.dat.gz, ..., all the way through 2011-12-31.tweets.dat.gz. 基本上,我正在寻找能够循环遍历2011-01-01.tweets.dat.gz,2011-01-02.tweets.dat.gz,......的代码,一直到2011-12-31。 tweets.dat.gz。
What I'm imagining now is somehow telling the program to loop over all files with the name 2011-*.tweets.dat.gz, but I'm not sure exactly how that would work or how to structure it, or even if the * syntax is correct. 我现在想象的是以某种方式告诉程序循环使用名称2011 - * .tweets.dat.gz的所有文件,但我不确定这将如何工作或如何构建它,或者即使*语法是正确的。
Any tips? 有小费吗?
Use the datetime
module: 使用datetime
模块:
>>> from datetime import datetime,timedelta
>>> d = datetime(2011,1,1)
while d < datetime(2012,1,1) :
filename = "{}{}".format(d.strftime("%Y-%m-%d"),'.tweets.dat.gz')
print filename
d = d + timedelta(days = 1)
...
2011-01-01.tweets.dat.gz
2011-01-02.tweets.dat.gz
2011-01-03.tweets.dat.gz
2011-01-04.tweets.dat.gz
2011-01-05.tweets.dat.gz
2011-01-06.tweets.dat.gz
2011-01-07.tweets.dat.gz
2011-01-08.tweets.dat.gz
2011-01-09.tweets.dat.gz
2011-01-10.tweets.dat.gz
...
...
2011-12-27.tweets.dat.gz
2011-12-28.tweets.dat.gz
2011-12-29.tweets.dat.gz
2011-12-30.tweets.dat.gz
2011-12-31.tweets.dat.gz
Easiest way is indeed with a glob: 最简单的方法确实是用glob:
import from glob import iglob
for pathname in iglob("/path/to/folder/2011-*.tweets.dat.gz"):
print pathname # or do whatever
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.