2011-11-10 16 views
5

Tüm alt dizinleri ve dosyaları bir dizin ağacında listelemeyi biliyorum. Ama tüm yeni oluşturulan dosyaları, (ve eğer mümkünse) silinmiş dosyaları kök dizininden başlayarak bir dizin ağacındaki tüm dizinlerde listelemenin yolunu arıyorum.Yeni oluşturulan, değiştirilen ve silinmiş dosyaların tümünü/dizininden başlayarak tüm dizinleri/alt dizinleri bulmak için Python kodu

+0

Lütfen yeni oluşturduğunuz şeyin ne olduğunu belirtin. Son saat içinde? Son gün? Bir yıldan beri mi? Bir dizin ağacı oluşturmayı biliyorsanız, neden dosya özelliklerine erişmek için 'os.lstat' kullanmıyorsunuz? – hochl

+0

Son saat içinde .... – nsh

+0

sonra 'st = os.lstat (dosyayolu)' ve 'st.st_mtime' alanını kullanın ve şu andaki zamanın farkının 1800'den az olup olmadığını kontrol edin - işte bu kadar. – hochl

cevap

11
Her dosyanın "mtime" bakarak Son yarım saatte oluşturulmuş veya değiştirilmiş tüm dosyaları bulabiliriz

:

import os 
import datetime as dt 

now = dt.datetime.now() 
ago = now-dt.timedelta(minutes=30) 

for root, dirs,files in os.walk('.'): 
    for fname in files: 
     path = os.path.join(root, fname) 
     st = os.stat(path)  
     mtime = dt.datetime.fromtimestamp(st.st_mtime) 
     if mtime > ago: 
      print('%s modified %s'%(path, mtime)) 

silinen dosyaların bir listesini oluşturmak için ayrıca olurdu 30 dakika önce dosya listesi var.


daha sağlam bir alternatif git gibi bir düzeltme kontrol sistemi kullanmaktır. Dizindeki tüm dosyaların bir taahhütte bulunması, bir anlık görüntü oluşturmak gibidir. Sonra

komutu, son işlemden bu yana değişen tüm dosyaları listeleyecektir. Bu da silinmiş dosyaları listeler.

+0

Yukarıdaki kodu çalıştırarak, aşağıdaki hatayı verir: Traceback (son çağrı son): Dosya "tsck.py", satır 13, içinde? print ('{p} değiştirilmiş {m}'. Format (p = yol, m = m zaman)) AttributeError: 'str' nesnesi 'format' özelliğine sahip değil – nsh

+0

Çok yavaş, başka bir yol bulabiliriz, etkinleştirebiliriz Sistem yeni oluşturulan dosyaları günlüğe kaydetme ve ardından günlük dosyalarını ayrıştırma. veya daha iyi bir yol, yeni günlük girişi için bir tetikleyici eklemektir. – pylover

+0

@nsh: 'str.format', Python2.6'da tanıtıldı. Önceki sürümlerde, '% s' stili dizge biçimlendirmesini kullanabilirsiniz. Ne demek istediğimi göstermek için gönderimi düzenleyeceğim. – unutbu

0

bir göz atın

örnek

karşılaştırmak için bir geçici dosya oluşturmak "adam bulmak":

find/-type f -newerB tempFile

adamın kısmı bulmak

-newerXY reference 
      Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one 
      of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are place‐ 
      holders for other letters, and these letters select which time belonging to how reference is used for the comparison. 

      a The access time of the file reference 
      B The birth time of the file reference 
      c The inode status change time of reference 
      m The modification time of the file reference 
      t reference is interpreted directly as a time 
1
from tempfile import mkstemp 
import shutil 
import os 
import datetime as dt 
import sys 


# gets the time frame we are going to look back and builds a placeholder list to passover the info from our mtime to slay 
now=dt.datetime.now() 
ago=now-dt.timedelta(minutes=480) 
passover=[] 

# the '.' is the directory we want to look in leave it to '.' if you want to search the directory the file currently resides in 
for root,dirs,files in os.walk('.'): 
    for fname in files: 
     path=os.path.join(root,fname) 
     st=os.stat(path) 
     mtime=dt.datetime.fromtimestamp(st.st_mtime) 
     if mtime>ago: 
      passover.append(path) 


def slay(file_path, pattern, subst): 
    #Create temp file 
    fh, abs_path = mkstemp() 
    with open(abs_path,'w') as new_file: 
     with open(file_path) as old_file: 
      for line in old_file: 
       new_file.write(line.replace(pattern, subst)) 
    old_file.close() 
    #Remove original file 
    os.remove(file_path) 
    #Move new file 
    try: 
     shutil.move(abs_path, file_path) 
    except WindowsError: 
     pass 

#we pass the passover list to the slay command in a for loop in order to do muiltple replaces in those files. 
for i in passover: 
    slay(i,"String1","String2") 
+0

Bunu bir dir dosyasına bakmak ve son zaman içinde değiştirilen dosyaları seçmek için oluşturdum ve ardından bu dosyalardaki metni değiştirin. Bu senaryo etrafta uzanmıyordu ve yukarıdaki cevaptan bir araya getirmem gerekiyordu, bu yüzden başka birinin onu aramaya gelebileceğini düşündüm. – Powerboy2

+0

Lütfen cevabınızı bu bilgi ile düzenleyin. Ayrıca, tam bir cevap, ne yaptığını açıklayan birkaç satır olmalıdır. Lütfen şu makaleyi okuyun: [İyi bir cevabı nasıl yazarım?] (Http://stackoverflow.com/help/how-to-answer) – Mariano

İlgili konular