简体   繁体   English

列出名称包含python日期字符串的文件

[英]List files whose name contains a date string with python

I have this code to list files like '*20150304.csv' but returns nothing 我有这个代码列出像'* 20150304.csv'这样的文件但没有返回任何内容

#!/usr/bin/env python
dir_local = '/var/log/'

import os
import glob
from datetime import date, timedelta

today = date.today
yesterday = date.today() - timedelta(1)
YEST = yesterday.strftime('%Y%m%d')

glob_pattern='*YEST.csv'

for fname in glob.glob(dir_local + os.sep + glob_pattern):
    print fname

However, when i replace with 但是,当我替换

for fname in glob.glob('/var/log/' + os.sep + '*20150304.csv'):

I get the desired result though the date will be changing everyday and hence need to list new files. 虽然日期每天都在变化,但我需要列出新文件,我得到了理想的结果。 What can i do? 我能做什么?

I have read this link list is coming as blank in python but we do not seen to have the same problem. 我已经读过这个链接列表在python中显示为空白但我们没有看到有相同的问题。

You just need to generate the current date (or yesterday's date?) as a string. 您只需要将当前日期(或昨天的日期?)生成为字符串。 Try this: 尝试这个:

yesterday = datetime.date.today() - datetime.timedelta(days=1)
yesterday.strftime('%Y%m%d')

Now you can put that in your glob . 现在你可以将它放在你的glob

Replace: 更换:

glob_pattern='*YEST.csv'

with: 有:

glob_pattern = '*{0}.csv'.format(YEST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM