2008-10-03 9 views
78

निर्देशिका में सभी फ़ाइलों की सूची प्राप्त करने का सबसे अच्छा तरीका क्या है, दिनांक के अनुसार क्रमबद्ध [बनाया गया | संशोधित], एक विंडोज मशीन पर पाइथन का उपयोग कर?पाइथन में निर्माण तिथि द्वारा क्रमबद्ध निर्देशिका सूची कैसे प्राप्त करें?

उत्तर

41

यहां @Greg Hewgill's answer का एक और वर्बोज़ संस्करण है। यह प्रश्न आवश्यकताओं के अनुरूप है। यह सृजन और संशोधन तिथियों (कम से कम विंडोज़) के बीच एक अंतर बनाता है।

#!/usr/bin/env python 
from stat import S_ISREG, ST_CTIME, ST_MODE 
import os, sys, time 

# path to the directory (relative or absolute) 
dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.' 

# get all entries in the directory w/ stats 
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath)) 
entries = ((os.stat(path), path) for path in entries) 

# leave only regular files, insert creation date 
entries = ((stat[ST_CTIME], path) 
      for stat, path in entries if S_ISREG(stat[ST_MODE])) 
#NOTE: on Windows `ST_CTIME` is a creation date 
# but on Unix it could be something else 
#NOTE: use `ST_MTIME` to sort by a modification date 

for cdate, path in sorted(entries): 
    print time.ctime(cdate), os.path.basename(path) 

उदाहरण:

$ python stat_creation_date.py 
Thu Feb 11 13:31:07 2009 stat_creation_date.py 
+0

यह पूरी तरह से काम किया। मैं एक दूसरे के साथ दो निर्देशिका cdate की तुलना करने की कोशिश कर रहा हूँ। क्या दो सीडीट्स के बीच सेकंड की तुलना करने का कोई तरीका है? – Federer

+0

@malcmcmul: एपोक के बाद से 'सीडीएटी' सेकंड की फ्लोट संख्या है। – jfs

+3

यह काम करता है लेकिन सबसे संक्षिप्त समाधान http://stackoverflow.com/a/4500607/68534 – jmoz

87

मैं एक निर्देशिका में पिछले अद्यतन की गई फ़ाइलें निर्धारित करने के लिए एक अजगर स्क्रिप्ट के लिए अतीत में इस किया है:

import glob 
import os 

search_dir = "/mydir/" 
# remove anything from the list that is not a file (directories, symlinks) 
# thanks to J.F. Sebastion for pointing out that the requirement was a list 
# of files (presumably not including directories) 
files = filter(os.path.isfile, glob.glob(search_dir + "*")) 
files.sort(key=lambda x: os.path.getmtime(x)) 

है कि आप फ़ाइल mtime के आधार पर जो खोज रहे हैं क्या करना चाहिए।

संपादित: ध्यान दें कि आप भी() os.listdir उपयोग कर सकते हैं glob.glob के स्थान पर() अगर वांछित - कारण मैं अपने मूल कोड में ग्लोब का इस्तेमाल किया है कि मैं केवल खोज के लिए ग्लोब का उपयोग करना चाहते किया गया था फ़ाइल एक्सटेंशन के किसी विशेष सेट वाली फ़ाइलों के लिए, जो ग्लोब() बेहतर अनुकूल था।

import os 

search_dir = "/mydir/" 
os.chdir(search_dir) 
files = filter(os.path.isfile, os.listdir(search_dir)) 
files = [os.path.join(search_dir, f) for f in files] # add path to each file 
files.sort(key=lambda x: os.path.getmtime(x)) 
+0

ग्लोब() अच्छा है, लेकिन ध्यान रखें कि यह फ़ाइलों को छोड़ देता है एक अवधि के प्रारंभ। * निक्स सिस्टम ऐसी फाइलों को छिपाते हैं (इस प्रकार उन्हें लिस्टिंग से छोड़कर), लेकिन विंडोज़ में वे सामान्य फाइलें हैं। – efotinis

+0

ये समाधान सूची से डीआईआर को बाहर नहीं करते हैं। – Constantin

+0

आपके os.listdir समाधान में os.path.join गुम है: files.sort (lambda x, y: cmp (os.path.getmtime (os.path.join (search_dir, x)), os.path। getmtime (os.path.join (search_dir, y))) –

-4

शायद तुम खोल आदेशों का उपयोग करना चाहिए: यहाँ listdir उपयोग करने के लिए यह क्या इस तरह दिखाई देगा। यूनिक्स/लिनक्स में, सॉर्ट के साथ पाइप ढूंढें शायद आप जो चाहते हैं उसे करने में सक्षम होंगे।

17

यहाँ एक एक लाइनर है:

import os 
import time 
from pprint import pprint 

pprint([(x[0], time.ctime(x[1].st_ctime)) for x in sorted([(fn, os.stat(fn)) for fn in os.listdir(".")], key = lambda x: x[1].st_ctime)]) 

यह os.listdir() फ़ाइल नामों की एक सूची प्राप्त करने के लिए कहता है, तो हर एक के लिए os.stat() कॉल निर्माण के समय प्राप्त करने के लिए है, तो एक तरह निर्माण के समय के खिलाफ।

ध्यान दें कि यह विधि केवल प्रत्येक फ़ाइल के लिए os.stat() को कॉल करती है, जो इसे प्रत्येक तुलना के लिए प्रत्येक तुलना के लिए कॉल करने से अधिक कुशल होगी।

def getfiles(dirpath): 
    a = [s for s in os.listdir(dirpath) 
     if os.path.isfile(os.path.join(dirpath, s))] 
    a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s))) 
    return a 

सबसे पहले, हम फ़ाइल नामों की एक सूची का निर्माण:

+0

जो कि शायद ही कभी पाइथोनिक है, हालांकि यह नौकरी को हल करता है (अस्वीकरण: कोड का परीक्षण नहीं किया गया)। –

+0

यह समाधान सूची से डीआईआर को बाहर नहीं करता है। – Constantin

+0

@ कॉन्स्टेंटिन: यह सच है, लेकिन एक त्वरित [... अगर stat.S_ISREG (x)] इसे संभाल लेगा। –

15

यहाँ मेरी संस्करण है। isfile() निर्देशिकाओं को छोड़ने के लिए प्रयोग किया जाता है; निर्देशिकाओं को शामिल किए जाने पर इसे छोड़ा जा सकता है। फिर, हम संशोधित दिनांक का उपयोग कुंजी के रूप में सूची में क्रमबद्ध करते हैं।

4
sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.stat(p).st_mtime) 

इसके बजाय आप os.path.isfile साथ छानने की os.walk('.').next()[-1] इस्तेमाल कर सकते हैं, लेकिन यह है कि सूची में मृत सिमलिंक छोड़ देता है, और os.stat उन पर विफल हो जाएगा।

+0

हालांकि यह केवल वर्तमान निर्देशिका में काम करता है। – Tom

13

एक os.path.getmtime समारोह है कि युग के बाद से सेकंड की संख्या देता है और os.stat की तुलना में तेजी से किया जाना चाहिए नहीं है।

os.chdir(directory) 
sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime) 
1

इस के लिए एक बुनियादी कदम है सीखना:

import os, stat, sys 
import time 

dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.' 

listdir = os.listdir(dirpath) 

for i in listdir: 
    os.chdir(dirpath) 
    data_001 = os.path.realpath(i) 
    listdir_stat1 = os.stat(data_001) 
    listdir_stat2 = ((os.stat(data_001), data_001)) 
    print time.ctime(listdir_stat1.st_ctime), data_001 
8

यहाँ फिल्टर के बिना ग्लोब का उपयोग कर आप तारीख के क्रम में एक निश्चित विस्तार (अजगर 3) के साथ फ़ाइलों को पढ़ना चाहते हैं मेरा उत्तर है।

import os  

path = '/path/to/files/' 
name_list = os.listdir(path) 
full_list = [os.path.join(path,i) for i in name_list] 
time_sorted_list = sorted(full_list, key=os.path.getmtime) 

print time_sorted_list 

# if you want just the filenames sorted, simply remove the dir from each 
sorted_filename_list = [ os.path.basename(i) for i in time_sorted_list] 
print sorted_filename_list 
14
बदलते निर्देशिका के बिना

जब फ़ाइल मौजूद नहीं है, अब() का उपयोग किया जाता है, और symlink सूची के बहुत अंत में जाएगा।

1

एलेक्स कोवेंट्री का जवाब अगर फ़ाइल unexistent फाइल करने के लिए एक सिमलिंक है एक अपवाद का उत्पादन करेगा, निम्नलिखित कोड है कि इसका जवाब को सही:

import time 
import datetime 
sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.path.exists(p) and os.stat(p).st_mtime or time.mktime(datetime.now().timetuple()) 

dataset_path='/mydir/' 
files = glob.glob(dataset_path+"/morepath/*.extension") 
files.sort(key=os.path.getmtime) 
1

अजगर में 3.5+

from pathlib import Path 
sorted(Path('.').iterdir(), key=lambda f: f.stat().st_mtime) 
संबंधित मुद्दे