基于创建日期的python排序文件

时间:2016-01-04 01:07:16

标签: python file sorting

import os, sys
import os.path, time
path=os.getcwd()
def file_info(directory):
    file_list = []
    for i in os.listdir(directory):
        a = os.stat(os.path.join(directory,i))
        file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
    return file_list


print file_info(path)

问题

  • 我如何在新行中显示每个列表项并且格式不错
  • 如何根据上次修改
  • 对文件/目录列表进行排序
  • 如何根据创建日期对文件/目录列表进行排序

1 个答案:

答案 0 :(得分:1)

这是使用格式函数进行一些不错打印的程序:

import os
import time

path = os.getcwd()

def file_info(directory):
    file_list = []
    for i in os.listdir(directory):
        a = os.stat(os.path.join(directory,i))
        file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
    return file_list


file_list = file_info(path)

for item in file_list:
    line = "Name: {:<20} | Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
    print(line)

以下是一些代码,其中访问时间使用了排序功能。代码没有经过优化,但它非常易读,您应该能够理解它。

import os
import time

path = os.getcwd()

def file_info(directory,sortLastModifiedOrNaw=False):
    file_list = []
    currentMin = 0 #This is the variable that will track the lowest digit
    for i in os.listdir(directory):
        a = os.stat(os.path.join(directory,i))
        if sortLastModifiedOrNaw == True: #If you would like to sort.
            if a.st_atime > currentMin: #Check if this is bigger than the current minimum. 
                currentMin = a.st_atime #If it is we update the current minimum
                #Below we append so that it ends up in the end of the list
                file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
            else: #If it is smaller, it should be in the front of the list so we insert it into position 0. 
                file_list.insert(0,[i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
        else: #If you would not like to sort
            file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
    return file_list


file_list = file_info(path)

print("Unsorted Example")
for item in file_list:
    line = "Name: {:<20} | Date Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
    print(line)

print("\nSorted example using last modified time")
file_list = file_info(path,sortLastModifiedOrNaw=True)

for item in file_list:
    line = "Name: {:<20} | Date Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
    print(line)

示例输出:

Unsorted Example
Name: .idea                | Date Last Accessed: Sun Jan  3 21:13:45 2016 | Date Created: Sun Jan  3 21:13:14 2016
Name: blahblah.py          | Date Last Accessed: Sun Jan  3 21:13:48 2016 | Date Created: Sun Jan  3 21:13:48 2016
Name: testhoe1.py          | Date Last Accessed: Sun Jan  3 19:09:57 2016 | Date Created: Sun Jan  3 18:52:06 2016

Sorted example using last modified time
Name: testhoe1.py          | Date Last Accessed: Sun Jan  3 19:09:57 2016 | Date Created: Sun Jan  3 18:52:06 2016
Name: .idea                | Date Last Accessed: Sun Jan  3 21:13:45 2016 | Date Created: Sun Jan  3 21:13:14 2016
Name: blahblah.py          | Date Last Accessed: Sun Jan  3 21:13:48 2016 | Date Created: Sun Jan  3 21:13:48 2016

快乐优化! #如果您将第12行 atime 更改为 ctime ,它将根据创建时间进行排序。