2013-05-16 3 views
5

यह एक चुनौती है और साथ ही एक प्रश्न है:पायथन: निर्देशिका में सभी फ़ाइलों और फ़ोल्डरों की सूची, सृजन का समय, अंतिम संशोधन का समय प्राप्त करें। सिस्टम स्वतंत्र समाधान?

मेरे पास डेटा फ़ाइलों का एक फ़ोल्डर है। मैं जानकारी की सूचियों के निम्न सूची चाहते हैं:

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?

हालांकि: मैं वहाँ एक बेहतर, अधिक फिर से प्रयोग करने योग्य समाधान जो Windows, और Linux पर काम करता है मौजूद होना चाहिए लग रहा है।

+1

http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date- टाइम्स-इन-पायथन? आरक्यू = 1 –

उत्तर

5

मुझे इस तथ्य के बारे में पता है कि os.statwindows और linux दोनों पर अच्छी तरह से काम करता है।

प्रलेखन 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 Sure, मेरे अतिथि बनें। लेकिन इस प्रश्न से जुड़े किसी भी धागे, साथ ही संबंधित धागे से जुड़े हुए हैं। – enginefree

संबंधित मुद्दे