猿问

Python:获取空文件以及非空最新文件的列表

我文件夹中的文件:文件格式- <ABC>_<123>_<CURRENT_DATE>.csv


Example:

1.

  ABC_123_20180802 - Empty File

  ABC_123_20180730 - Empty File

  ABC_123_20180725 - Non-Empty File 


2.  

  EFG_456_20180802 - Empty File

  ABC_456_20180601 - Non-Empty File  

我们的Python版本是2.6,并希望打印两件事。


1.特定日期文件夹中的空文件(csv)列表。开始此操作以列出空文件:


path="C:\Users\\"

for f in os.listdir(path):

    file=path+'\\'+f

    if (os.stat(file).st_size == 0):

        print(file)


Expected Output:

    ABC_123_20180802 - File Empty

    EFG_456_20180802 - File Empty

2.如果当前日期文件为空,则列出最后一个非空文件(最新日期)。


Expected Output:

    ABC_123_20180725 - File Non-Empty

    ABC_456_20180601 - File Non-Empty

如何在文件夹中列出给定日期的空文件?其次,如何查找文件夹中所有最新的非空文件(任何日期)?


江户川乱折腾
浏览 169回答 1
1回答

Helenr

似乎您已经在使用回答问题所需的所有工具。latest_non_empty = Nonepath="C:\Users\\"for f in os.listdir(path):&nbsp; &nbsp; file=path+'\\'+f&nbsp; &nbsp; if (os.stat(file).st_size > 0):&nbsp; &nbsp; &nbsp; &nbsp; if latest_none_empty:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if lastest_none_empty[0] < os.stat(file).st_mtime:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latest_non_empty_file = (os.stat(file).st_mtime, file)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; latest_non_empty_file = (os.stat(file).st_mtime, file)
随时随地看视频慕课网APP

相关分类

Python
我要回答