私: アップグレードの可能性なしで Python 2.3.3 を実行しています。Python の経験はあまりありません。私の学習方法は、グーグルで調べて大量のスタックオーバーフローを読むことです。
背景: 2 つのディレクトリを引数として取り、2 つのディレクトリ内で見つかったすべてのファイルの比較/差分を実行することを目的とした python スクリプトを作成しています。ディレクトリにはサブディレクトリがあり、これも差分に含める必要があります。各ディレクトリはリストであり、サブディレクトリはネストされたリストなどです...
the two directories:
oldfiles/
a_tar_ball.tar
a_text_file.txt
nest1/
file_in_nest
nest1a/
file_in_nest
newfiles/
a_tar_ball.tar
a_text_file.txt
nest1/
file_in_nest
nest1a/
問題: oldfiles 内のすべてのファイルは newfiles に存在するはずなので、通常はすべてうまくいくはずですが、上記の例では、'file_in_nest' の 1 つが 'newfiles/' にありません。どのファイルが欠落しているかを知らせるエラー メッセージを出力したいのですが、「比較」関数の現在のインスタンスの下にあるコード構造を使用している場合、最も近いディレクトリ以外のディレクトリはわかりません。ファイルとディレクトリに関する情報を再帰ラダーに送信して、情報を追加するエラー処理が組み込まれているのではないかと思います。「oldfiles」には2つの「file_in_nest」があるため、欠落しているファイルのファイル名を印刷するだけでは、どれがどれであるかわかりません
def compare(file_tree)
for counter, entry in enumerate(file_tree[0][1:]):
if not entry in file_tree[1]
# raise "some" error and send information about file back to the
# function calling this compare, might be another compare.
elif not isinstance(entry, basestring):
os.chdir(entry[0])
compare(entry)
os.chdir('..')
else:
# perform comparison (not relevant to the problem)
# detect if "some" error has been raised
# prepend current directory found in entry[0] to file information
break
def main()
file_tree = [['/oldfiles', 'a_tar_ball.tar', 'a_text_file.txt', \
[/nest1', 'file_in_nest', [/nest1a', 'file_in_nest']], \
'yet_another_file'], \
['/newfiles', 'a_tar_ball.tar', 'a_text_file.txt', \
[/nest1', 'file_in_nest', [/nest1a']], \
'yet_another_file']]
compare(file_tree)
# detect if "some" error has been raised and print error message
これは、読む以外のスタックオーバーフローでの私の最初の活動です。質問を改善する必要があるかどうか教えてください!
// ステファン