sys.argvを使用してコマンドラインから引数を調べる必要があります。これは私の場合はファイル名になります。私のコードは以下の通りです。私はargparseをインポートすることは許されておらず、sysだけを使用することが許されています。私はここで何か間違っていることを知っています。助けをお待ちしています。sys.argvをPythonでテキストファイルを開く関数に追加するには
def get_inputfile_object():
'''
Check the command line for an argument. If one was there, use it as the
filename. Otherwise, use DEFAULT_INPUT_FILENAME. Open the file.
If file is successfully opened:
print MSG_OPENING_FILE
Return: a file object for that file
If the file cannot be opened:
print MSG_ERROR_OPENNING_FILE
Return: True
'''
if sys.argv > 1:
pass
else:
input_filename = DEFAULT_INPUT_FILENAME
input_filename = DEFAULT_INPUT_FILENAME
if os.path.isfile(input_filename) and os.access(input_filename,os.R_OK):
#Prints the opening file message, and the name of the file
print (MSG_OPENING_FILE,input_filename)
return open(input_filename,'r')
else:
print (MSG_ERROR_OPENING_FILE)
return True
'lenの場合(sys.argvの)> 1:' – Evert
もargparseチェックアウトします。https://ドキュメントを.python.org/3/library/argparse.html – bcorso
この猿ビジネスの大部分を担当する[fileinput](https://docs.python.org/2/library/fileinput.html)あなたのために。 – dawg