2011-11-10 13 views
5

ディレクトリツリー内のすべてのサブディレクトリとファイルをリストする方法を知っています。しかし、私はルートディレクトリから始まるディレクトリツリーのすべてのディレクトリで、新しく作成されたすべてのファイル、変更されたファイル、および(可能であれば)削除されたファイルをリストする方法を探しています。/ディレクトリから始まるすべてのディレクトリ/サブディレクトリ内に新しく作成、変更、削除されたすべてのファイルを見つけるPythonコード

+0

新たに作成されたものを指定してください。最後の1時間以内に?最終日?一年以来?ディレクトリツリーを構築する方法を知っているなら、 'os.lstat'を使ってファイルのプロパティにアクセスするのはなぜですか? – hochl

+0

最後の1時間以内に.... – nsh

+0

'st = os.lstat(filepath)'と 'st.st_mtime'フィールドを使って現在時刻との差が1800未満であるかどうかを確認します。 – hochl

答えて

11
あなたは、各ファイルの「ファイルのmtime」を見て、最後の半時間で作成または変更されたすべてのファイルを見つけることができる

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)) 

が削除されたファイルのリストを生成するために、あなたはまた、必要があるだろう30分前にファイルのリストがあります。


より堅牢な代替案は、gitようなリビジョン管理システムを使用することです。ディレクトリ内のすべてのファイルをコミットすることは、スナップショットの作成に似ています。その後、コマンドは、最後のコミット後に変更されたすべてのファイルを一覧表示します。

git status -s 

これにより、削除されたファイルも一覧表示されます。

+0

上記のコードを実行すると、次のエラーが発生します。トレースバック(最新の最後の呼び出し): ファイル "tsck.py"、13行目? AttributeError: 'str'オブジェクトに属性 'format'がありません – nsh

+0

遅いですが、別の方法を見つけることができます。新しく作成したファイルをログに記録し、ログファイルを解析します。またはより良い方法は、新しいログentry.mayのヘルプのトリガーを追加することです! – pylover

+0

@nsh: 'str.format'はPython 2.6で導入されました。以前のバージョンでは、 '%s'形式の文字列書式を使うことができました。私は自分の投稿を編集して、私が何を意味するのかを示します。 – unutbu

0

例を比較するために、一時ファイルを作成する "男は見つける" を見てみましょう:

find/-type f -newerB tempFile

人の一部が

-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

私はdirを調べ、最後の時間内に変更されたファイルを選択し、それらのファイルのテキストを置き換えるためにこれを構築しました。このスクリプトは敷設されていなかったので、私は上記の答えからそれをまとめなければならなかったので、他の誰かがそれを探しに来るかもしれないと考えました。 – Powerboy2

+0

この情報であなたの答えを編集してください。また、完全な答えには、それが何をしているのかを説明する数行が含まれていなければなりません。次の記事を読んでください:[どのように私は良い答えを書くのですか?](http://stackoverflow.com/help/how-to-answer) – Mariano

関連する問題