Python:获取空文件列表加上非空最新文件
Python: Get the list of empty files plus non-empty latest files
我的文件夹中的文件:
文件模式 - <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.The 特定日期文件夹中的空文件 (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.In 如果当天文件为空,则列出最后一个非空文件(最新日期)。
Expected Output:
ABC_123_20180725 - File Non-Empty
ABC_456_20180601 - File Non-Empty
如何列出文件夹中给定日期的空文件?
二、如何查找一个文件夹中所有最新的非空文件(任意日期)?
看来您已经在使用所有需要的工具来回答您的问题。
latest_non_empty = None
path="C:\Users\"
for f in os.listdir(path):
file=path+'\'+f
if (os.stat(file).st_size > 0):
if latest_none_empty:
if lastest_none_empty[0] < os.stat(file).st_mtime:
latest_non_empty_file = (os.stat(file).st_mtime, file)
else:
latest_non_empty_file = (os.stat(file).st_mtime, file)
我的文件夹中的文件:
文件模式 - <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.The 特定日期文件夹中的空文件 (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.In 如果当天文件为空,则列出最后一个非空文件(最新日期)。
Expected Output:
ABC_123_20180725 - File Non-Empty
ABC_456_20180601 - File Non-Empty
如何列出文件夹中给定日期的空文件? 二、如何查找一个文件夹中所有最新的非空文件(任意日期)?
看来您已经在使用所有需要的工具来回答您的问题。
latest_non_empty = None
path="C:\Users\"
for f in os.listdir(path):
file=path+'\'+f
if (os.stat(file).st_size > 0):
if latest_none_empty:
if lastest_none_empty[0] < os.stat(file).st_mtime:
latest_non_empty_file = (os.stat(file).st_mtime, file)
else:
latest_non_empty_file = (os.stat(file).st_mtime, file)