1

問題 ; ディレクトリ構造から特定の日付以降に変更されたファイル/フォルダーを見つけて、これらを別の場所にコピーします (タイトルにも明確に記載されています) (:

以下により、

def mod():
     """Find files modified today, given a file path."""
     latest = 0
     now = time.strftime('%Y-%m-%d', time.localtime())
     dir = "/home/y33t/"
     for fname in os.listdir(dir):
         if fname.endswith(''):
             modtime = os.stat(os.path.join(dir, fname)).st_mtime
             if modtime > latest:
                 latest = modtime
                 out = time.strftime('%Y-%m-%d', time.localtime(latest))
                 if out == now:
                     print fname, "has changed today. "
                 else:
                     pass

特定の日付に変更されたファイルを特定し、それらを特定の場所にコピーできます。私が達成したいのは、ディレクトリ構造も維持することです。例は次のとおりです。

/testfolder
..somefile1
..somefile2
../testfolder2
....somefile3
....somefile4

等々...

somefile3 が指定された日付に変更され、別の場所に保存するとしますが、保存中はカスケード ディレクトリ構造も維持する必要があります。どうすればエレガントな方法でこれを達成できますか?

4

1 に答える 1

1

コピーする前に、読みを解く必要があります。os.listdirでは1 つのレベルしかos.walk表示されませんが、各ファイルをあらゆる深さで調べることができます。

コピーするには、最初に を使用os.makedirs(target-path)してルートからターゲット フォルダーまでのすべてのフォルダーを任意の深さで作成し、次に を使用shutil.copyしてファイルをコピーします。

于 2012-06-29T09:35:57.873 に答える