2013-05-16 5 views
5

이 질문은 물론 질문입니다.Python : 디렉토리의 모든 파일과 폴더 목록, 생성 시간, 마지막 수정 시간을 가져옵니다. 시스템 독립적 인 솔루션?

데이터 파일의 폴더가 있습니다. 나는 다음과 같은 정보 목록의 목록 싶어 : 나는 정보를 한 후

Filename:  Created:      Last modified: 

Information = 
[ 
[datafile1, Mon Mar 04 10:45:24 2013, Tue Mar 05 12:05:09 2013], 
[datafile2, Mon Mar 04 11:23:02 2013, Tue Apr 09 10:57:55 2013], 
[datafile2.1, Mon Mar 04 11:37:21 2013, Tue Apr 02 15:35:58 2013], 
[datafile3, Mon Mar 04 15:36:13 2013, Thu Apr 18 15:03:25 2013], 
[datafile4, Mon Mar 11 09:20:08 2013, Mon May 13 16:30:59 2013] 
] 

은 내가 나 자신을 정렬 할 수 있습니다.

def get_information(directory): 
    . 
    . 
    . 
    return Information 

이 게시물이 유용하다 :

1) How do you get a directory listing sorted by creation date in python?

2) Sorting files by date

3) How to get file creation & modification date/times in Python?

4) Python: sort files by datetime in more details

는 사람이 기능을 쓸 수

5) Sorting files by date

6) How do I get the modified date/time of a file in Python?

그러나 : 나는 윈도우 및 리눅스에서 작동 더 나은, 더 재사용 가능한 솔루션이 존재해야합니다 생각합니다.

+1

http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date- times-in-python? rq = 1 –

답변

5

사실 os.statwindowslinux 모두에서 잘 작동합니다.

문서 here 그러나

, 당신의 기능에 맞게, 당신이 할 수 있습니다 : 당신은 파일 생성 시간에 대한 가장 최근의 액세스 및 st_ctime에 액세스 할 수 st_atime을 사용할 수 있습니다

.

import os,time 

def get_information(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 get_information("/") 

나는 mac에 그리고 난이 얻을,

[['.dbfseventsd', 'Thu Apr 4 18:39:35 2013', 'Thu Apr 4 18:39:35 2013'], ['.DocumentRevisions-V100', 'Wed May 15 00:00:00 2013', 'Sat Apr 13 18:11:00 2013'],....] 
+0

예! 확인 - 이것은 윈도우에서도 작동하며 동일한 출력을 생성합니다. – Doug

+0

이 답변은 다른 스레드에서 알아야합니다. 우리는 그것을 링크하거나 복사해야합니다. – Doug

+0

@Doug 물론, 제 손님이 되십시오. 그러나이 질문에 연결된 모든 스레드는 동시에 해당 스레드에 연결됩니다. – enginefree