我想用Python遍历,一个文件夹下的所有excel文件,并获取所有Excel文件(xlsx后缀的)的创建时间,
用os.path.walk()方法和os.path.getctime(path),一直没有成功,请各位指点下
1
直接上代码和结果
import time
import os
if __name__ == "__main__":
path = "D:/test"
for root, dirs, files in os.walk(path):
for file in files:
if file.endswith(".xlsx"):
file_path = os.path.join(root, file)
ctime = os.path.getctime(file_path)
ctime = time.localtime(ctime)
ctime = time.strftime("%Y-%m-%d %H:%M:%S", ctime)
print(file, ctime)
import os
from time import ctime
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
path = os.path.join(root, name)
if os.path.splitext(path)[1] == '.xlsx':
print(path, ctime(os.path.getctime(path)))
>>> import os, time
>>> base_path = r'D:\Xufive\tx\res'
>>> for root, dirs, files in os.walk(base_path, topdown=False):
for name in files:
if os.path.splitext(name)[1] == '.xlsx':
fn = os.path.join(root, name)
dt = time.localtime(os.stat(fn).st_mtime)
print(fn, time.strftime("%Y-%m-%d %X", dt))
D:\Xufive\tx\res\out\C.xlsx 2022-09-02 08:50:48
D:\Xufive\tx\res\A.xlsx 2022-09-02 08:47:06
D:\Xufive\tx\res\B.xlsx 2022-09-02 08:47:19