スクリプトが含まれているディレクトリから始まるすべてのサブディレクトリでこのスクリプトを実行する場合は、代わりに次の方法で実行してみてください。
import os
for path, directories, files in os.walk(os.path.dirname(os.path.realpath(__file__))):
print path, directories, files
txt_files = [arbitrary_file for arbitrary_file in files if arbitrary_file[-4:].lower() == ".txt"]
#run your python here
txt_files = [txt_file for arbitrary_file in files if arbitrary_file[]
元のコードが次の場合:
import sys
text_files_to_process = #Do Something with sys.argv - or whatever you're using to parse your arguments.
with open("res.txt", "w") as f:
#do something with all the text files, and write the output to res.txt.
for text_file in text_files_to_process:
with open(text_file) as tf:
for line in tf:
#whatever your text processing is
tf.write("something")
次に、次のように変更します。
import os
for path, directories, files in os.walk(os.path.dirname(os.path.realpath(__file__))):
print path, directories, files
txt_files = [arbitrary_file for arbitrary_file in files if arbitrary_file[-4:].lower() == ".txt"]
txt_files = [txt_file for arbitrary_file in files if arbitrary_file[]
with open("res.txt", "w") as f:
#do something with all the text files, and write the output to res.txt.
for text_file in txt_files:
with open(text_file) as tf:
for line in tf:
#whatever your text processing is
tf.write("something")