如何在os path(python)中添加条件以仅列出2012年之后的文件
我有下面列出文件夹和子文件夹中所有文件的代码。我想添加代码,只列出2012年后修改的文件如何在os path(python)中添加条件以仅列出2012年之后的文件,python,list,os.path,Python,List,Os.path,我有下面列出文件夹和子文件夹中所有文件的代码。我想添加代码,只列出2012年后修改的文件 path=r'c:\xx\download' files_sorted_by_date = [] filenm=[] filept=[] creation=[] filepaths=[] for root,dirs,files in os.walk(path): for file in files: filepaths.append(os.path.join(root,file))
path=r'c:\xx\download'
files_sorted_by_date = []
filenm=[]
filept=[]
creation=[]
filepaths=[]
for root,dirs,files in os.walk(path):
for file in files:
filepaths.append(os.path.join(root,file))
file_statuses = [(os.stat(filepath), filepath) for filepath in filepaths]
files = ((status[stat.ST_MTIME], filepath) for status, filepath in file_statuses if stat.S_ISREG(status[stat.ST_MODE]))
for creation_time, filepath in sorted(files):
creation_date = time.ctime(creation_time)
filename = os.path.basename(filepath)
filept.append(filepath)
filenm.append(filename)
creation.append(creation_date)
files_sorted_by_date.append(creation_date + "," + filename)
#print(files_sorted_by_date)
df =pd.DataFrame(files_sorted_by_date,columns=['files'])
使用
os.path.getmtime(path)
您可以使用time.localtime(os.path.getmtime(path))
输出应该是这样的(2006,8,17,12,6,3,3,229,1)
现在可以得到元组的第一个值(年份)
year = time.localtime(os.path.getmtime(path))[0]