私はPythonスクリプトにかなり慣れていないので、ディレクトリとサブディレクトリ内のファイル名を確認したいと考えています. 検証では大文字と小文字を区別する必要があります。私はpython 2.6.5 OSを使用しています:win7とxp
次のユーザー入力を求めます。
prompt = "year"
year = raw_input(prompt)
prompt = "number"
number = raw_input(prompt)
ここから、次のファイルとフォルダーが存在し、それらのファイル名が正しいことを検索/確認したいと思います。
フォルダ構造:
..\foobar_(number)_version1\music
サブフォルダー「music」内のファイル
(year)_foobar_(number)_isnice.txt
(year)_itis(number)hot_today.txt
(year)_anything_is(number)possible.txt
(year)_something_{idont_want_to_check_this_part}_(number)_canbe_anything.txt
() または {} の間のものを除いて、アンダースコアを含むすべてのテキストは常に同じであるため、常に正しいはずです。ファイル名が正しいかどうかを報告するtxtファイルに結果を出力したい。
これをアーカイブするための最も論理的な方法は何ですか? ライブラリ ドキュメント fnmatch(.fnmatchcase)、RE、および os(.path.isfile) を読み、ここで例を検索しましたが、どこからどのように開始すればよいかわかりません。
誰かが私を正しい方向に向けることができますか?
[編集] スクリプトの基本が動作するようになり次第、参照用または他の人を助けるためにコードを投稿します。
[edit2] 私の最初の非 Hello World スクリプト
import os
import re
#output :
file_out = "H:\\output.txt"
f_out = open(file_out, 'w')
print "-------start-script----------"
#input
prompt = "enter 4 digit year: "
year = raw_input(prompt)
prompt = "enter 2 digit number: "
number = raw_input(prompt)
print "the chosen year is %s" % (year)
print "the chosen number is %s" % (number)
f_out.write ("start log!\n")
f_out.write ("------------------------------------------\n")
f_out.write ("the chosen year is %s\n" % (year))
f_out.write ("the chosen number is %s\n" % (number))
#part i'm working on
print "end script"
f_out.write ("------------------------------------------\n")
f_out.write ("end script\n")
#close file
f_out.close()