greeting.py
import argparse
import time
def arg_res():
parser = argparse.ArgumentParser()
parser.add_argument('--nickname',required=True)
args = parser.parse_args()
nickname = args.nickname
# エラーなし
greeting_sentence = f"僕のニックネームは{nickname}"
print(greeting_sentence)
# エラーあり
greeting_sentence = f"僕のニックネームは{nicknames}"
print(greeting_sentence)
return greeting_sentence
def test_main():
greeting_sentence = arg_res()
print(greeting_sentence)
if __name__=="__main__":
test_main()
run_greeting.py
import subprocess
from time import time
name_list = ["john","power","smith","jack"]
with open('out.txt', 'w') as fp:
for i in name_list:
print(i)
# コンソール実行時の場合に空白で区切る部分を分割してリストに入れる"
# python greeting.py --nickname XXXX の場合下記のようになる。
command = ["python","greeting.py","--nickname", f"{i}"]
proc = subprocess.Popen(command, stdout=fp,stderr=fp)
出力結果
out.txt
僕のニックネームはsmith
僕のニックネームはjohn
僕のニックネームはpower
Traceback (most recent call last):
File "greeting.py", line 28, in <module>
test_main()
File "greeting.py", line 24, in test_main
greeting_sentence = arg_res()
File "greeting.py", line 17, in arg_res
greeting_sentence = f"僕のニックネームは{nicknames}"
NameError: name 'nicknames' is not defined
Traceback (most recent call last):
File "greeting.py", line 28, in <module>
test_main()
File "greeting.py", line 24, in test_main
greeting_sentence = arg_res()
File "greeting.py", line 17, in arg_res
greeting_sentence = f"僕のニックネームは{nicknames}"
NameError: name 'nicknames' is not defined
Traceback (most recent call last):
File "greeting.py", line 28, in <module>
test_main()
File "greeting.py", line 24, in test_main
greeting_sentence = arg_res()
File "greeting.py", line 17, in arg_res
greeting_sentence = f"僕のニックネームは{nicknames}"
NameError: name 'nicknames' is not defined
僕のニックネームはjack
Traceback (most recent call last):
File "greeting.py", line 28, in <module>
test_main()
File "greeting.py", line 24, in test_main
greeting_sentence = arg_res()
File "greeting.py", line 17, in arg_res
greeting_sentence = f"僕のニックネームは{nicknames}"
NameError: name 'nicknames' is not defined