1

非 ASCII 文字を含むファイルのディレクトリ (およびそのサブディレクトリ) を調べる小さな Python プログラムを作成しました。

改善したいです。この「ディレクトリ」内の特定のファイルは、ZIP、DTA/OUT、OMX、SFD/SF3 などの可能性があることを知っています... 非 ASCII 文字を含むことが想定されているファイルです。したがって、これらが存在することを知り、ASCII 文字を含めてはならないファイルを選別します。私の最終的な目標は、非 ASCII 文字を含めてはならないファイルを見つけて削除することだからです (TB 価値のある不良セクタのある破損したディスク重要なデータの)。

私の考えでは、次のような Python の try/except ブロックの「except」部分にあるファイルをさらに調べることです。

try:
    content.encode('ascii')
    output.write(str(counter) + ", " + file + ", ASCII\n")
    print str(counter) + " ASCII file status logged successfully: " + file
    counter += 1 

except UnicodeDecodeError:
    output.write(str(counter) + ", " + file + ", non-ASCII\n")
    print str(counter) + " non-ASCII file status logged successfully: " + file
    counter += 1 

コードを書き始めたとき、ファイルが'.zip''.sfd'prか'.omx'などを尋ねてループするのは扱いにくいプログラムであり、永遠にかかることに気付きました。

1 つずつ検索する以外に、ファイル拡張子のグループを検索する方法はありますか? チェックするこれらの拡張子を含むファイルでしょうか?それとも私が考えていない何か?これがばかげた質問である場合は事前にお詫びしますが、Python には非常に多くのクールな関数があるため、役に立つ何かが欠けていると確信しています。

乾杯。

4

1 に答える 1

0

I figure since there aren't any answers I can go ahead and answer this myself with a partial answer. I basically took a different approach and looked for a particular file that is expected to be abundant for this share and then will do the same for each file. It's kind of hacky, but it will get the j ob done.

于 2011-11-21T19:08:54.680 に答える