Python 如何在系统文件夹的windows资源管理器中对创建日期为的子文件夹和文件的内容进行排序并进行更改(排序依据)?

Python 如何在系统文件夹的windows资源管理器中对创建日期为的子文件夹和文件的内容进行排序并进行更改(排序依据)?,python,pandas,directory,operating-system,Python,Pandas,Directory,Operating System,如何对主文件夹中的文件和子文件夹进行排序 我在桌面上有一个文件夹(buro),其中有1152个子文件夹和12个.csv文件。我希望所有这些都与修改日期一起排序。我在搜索中发现的是创建一个列表并对它们进行排序。但是我可以改变我们在windows资源管理器上看到的方式吗 你能帮忙吗?试试这个 import glob import os path = add_your_path files = [f for f in glob.glob(path + "**/*.csv", recursive=Tr

如何对主文件夹中的文件和子文件夹进行排序

我在桌面上有一个文件夹(buro),其中有1152个子文件夹和12个.csv文件。我希望所有这些都与修改日期一起排序。我在搜索中发现的是创建一个列表并对它们进行排序。但是我可以改变我们在windows资源管理器上看到的方式吗

你能帮忙吗?

试试这个

import glob
import os
path = add_your_path

files = [f for f in glob.glob(path + "**/*.csv", recursive=True)]
files.sort(key=os.path.getmtime)
#Path of your directory

file = "/home/cso/user/"

file_list = []
date_modified = []

for subdir, dirs, files in os.walk(file):
    for f in files:
        file_list.append(f)
        date_modified.append(time.ctime(os.path.getmtime(file)))



df = pd.DataFrame(list(zip(file_list, date_modified)), columns =['File', 'Date_modified'])
df = df.sort_values(by = 'Date_modified')

首先您应该创建一个函数来获取文件修改日期

import time
import os 


# format the data str
def time_stamp2time(timestamp):
    times_truct = time.localtime(timestamp)
    return time.strftime('%Y-%m-%d %H:%M:%S',times_truct)


def get_file_create_time(file_path):
    t = os.path.getctime(file_path)
    return time_stamp2time(t)


def get_file_modify_time(file_path):
    t = os.path.getmtime(file_path)
    return time_stamp2time(t)
然后您需要遍历文件夹以获取所有csv文件

import os
def get_all_file_path(folder_path):
    file_path_list = []
    for root, dirs, files in os.walk(folder_path):
        for name in files:
            file_path_list.append(os.path.join(root, name))
        # if you want get the sub folders path you can use it 
        #for name in dirs:
        #    print(os.path.join(root, name))
    return file_path_list
最后您可以使用
list.sort
sorted
来获得您想要的结果

# defined your desktop path 
your_desktop_path = "/home/username/" # maybe some others
folder_path= os.path.join(your_desktop_path ,"buro")
file_path_list = get_all_file_path(folder_path)
# you can use sorted instead
file_path_list.sort(key=get_file_modify_time)

此代码将给出文件夹buro中所有文件/文件夹的排序列表(基于修改时间):

import os
path_to_buro = "Desktop_path" # path where the folder buro is
buro = "buro" # name of the folder
sorted_files = [os.path.join(path_to_buro,buro,fold) for fold in os.listdir(os.path.join(path_to_buro,buro))]
sorted_files.sort(key=lambda x: os.path.getmtime(x),reverse=True)
要根据此设置编辑文件/文件夹日期时间,请在windows中测试:

year = 2019
month = 9
day = 11
hour = 15
minute = 30
second = 0

date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
modification_time = time.mktime(date.timetuple())

for fileLocation in sorted_files:
    os.utime(fileLocation, (modification_time , modification_time ))

你能描述一下日期的位置是什么意思吗?我从上周开始就知道了文件和文件夹的日期。我尝试了这个方法,但没有成功:def sorted_ls(path1):import glob import os path=path1 files=[f for f in glob.glob(path+“*/.csv”,recursive=True)]files.sort(key=os.path.getmtime)sorted_ls(“C:/Users/Elite/Desktop/kro”)我应该给'buro'变量分配什么?buro='buro'(文件夹名称)和path_to_buro=“绝对路径”其中,文件夹必须是'buro'(桌面路径),然后buro必须是'buro'?是的,请检查编辑。刚刚测试过,为我工作。使用“reverse”参数进行降序或升序排序谢谢。但问题是什么呢?这对我不起作用。我的路径为C:\Users\ABANISH\Desktop。正确吗?有了这个,我可以用python对列表进行排序,但它也会对文件夹进行更改吗?不,如果你想在windows上对文件夹进行排序,有列可以按修改后的数据进行排序,如果我没记错的话,我可以用python对列表进行排序,但它也会对文件夹进行更改吗?