2016-01-04 114 views
-3
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) 

问题蟒蛇类型的文件

  • 我怎么能显示出新的生产线和漂亮的一个很好的格式,每个列表项
  • 我怎么能进行排序依据的文件/目录列表在最后一次修改
  • 我怎么能基于creatation日期的文件/目录列表进行排序
+1

你有没有尝试过任何东西,看着['sorted()'](https://docs.python.org/3/library/functions.html#sorted)内置函数? –

+0

@KevinGuan我看着排序但无法排序 – Roujri

回答

1

她e是程序与使用格式函数一些很好的打印:

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行atimectime它将根据创建时间进行排序。

+0

如何根据创建日期或上次访问的文件进行排序? – Roujri

+0

我已经更新了答案@Roujri – abe

+1

'排序(file_list,key = lambda x:x [1])'和'sorted(file_list,key = lambda x:x [2])'有什么问题'在上次修改和创建日期而不是更改插入顺序?你不觉得你的代码有点复杂吗? –