简体   繁体   English

Python:如何迭代具有相似名称的几个文件(每个名称的变体是日期)?

[英]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.

相关问题 如何使用“ for”语句引用Python中多个具有相似名称的脚本? - How do I reference several scripts with similar names in Python with a “for” statement? 如何迭代python中的文件并导出多个输出文件 - how to iterate over files in python and export several output files Python:如何遍历循环更改其名称的文件? - Python: How to iterate over files changing their names in loop? 如何在Python中遍历“ gslist”? - how do I iterate over a “gslist” in Python? 如何遍历链接列表到多个文件,每个文件上都有特定范围? - How do I iterate over a list of links onto multiple files with a specific range on each file? 如何遍历包含字典的列表并检查Python中每个字典中键的值? - How do I iterate over a list containing dictionaries and check the values of the key in each of the dictionaries in Python? Python:如何在不知道子列表数目的情况下遍历每个子列表的第一个元素? - Python: how do i iterate over the first element of each sublist without knowing the number of sublists? 如何遍历具有相似名称的列,并检查它们是否相等? - How to iterate over columns, with similar names, and check whether they are equal? 如何使用 Python 遍历包含子目录的目录中的文件? - How do I iterate over files in a directory including sub directories, using Python? 如何在python上读取具有相似名称的文件,然后使用它们? - How can I read files with similar names on python and then work with them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM