I'm writing a code where I'm supposed to use "sys.argv" to read a text file. I want two different outputs depending on the input arguments. If the argument in the terminal is only "message.txt" I want the output to be:
Anna
Hello Everyone
Kalle
Hi there
Anna
Coffee break?
Pelle
Absolutely
Kalle
Sure
Anna
Great, see you downstairs in 5 minutes
But if the input is both "Lab2_1.py" and "message.txt" I want to send the text file to a function where it prints another output. The function works, I've tried it before.
I'm thinking some sort of if-statement for the input arguments but I can't get it to work. My main function now that sends the argument to the function is (this works the way I want it to):
messages = read_file(sys.argv[1])
name1 = input('Enter a name to search for: \n')
display_entry(name1, messages)
I was thinking something like this (doesn't work):
if len(sys.argv) == 1:
messages = read_file(sys.argv)
print(messages)
else:
messages = read_file(sys.argv)
name1 = input('Enter a name to search for: \n')
display_entry(name1, messages)
So that the first statement will print only the text file in its "original" form, like the first code block above, and the other statement will send the text file to a function and create another output. There will be maximum 2 arguments in the terminal.
Any ideas on how I can fix this? Thanks :)
Aucun commentaire:
Enregistrer un commentaire