My program takes 2 or 3 command line parameters:
-s is an optional parameter, indicating a switch in my program later on
-infile is the file input
-outfile is to be the written file
I need my program to print an error message and quit if any of the following happen:
- the user specifies an infile name that does not end with .genes
- the user specifics an outfile name that does not end with either .fa or .fasta
- the user provides less than 2, or more than 3, parameters
- the user’s first parameter starts with a dash, but is not ‘-s’
I have written:
def getGenes(spliced, infile, outfile):
spliced = False
if '-s' in sys.argv:
spliced = True
sys.argv.remove('-s')
infile, outfile = sys.argv[1:]
if not infile.endswith('.genes'):
print('Incorrect input file type')
sys.exit(1)
if not outfile.endswith('.fa' or '.fasta'):
print('Incorrect output file type')
sys.exit(1)
if not 2 <= len(sys.argv) <= 3:
print('Command line parameters missing')
sys.exit(1)
if sys.argv[1] != '-s':
print('Invalid parameter, if spliced, must be -s')
sys.exit(1)
However, something is conflicting with some of the conditionals, including the first and last one being contradictory due to the fact that s.argv[1] always unequal to ‘-s’ becuase if ‘s’ were present in argv, it was removed earlier. So I am not sure how to write this correctly…
sliced=Falseis not indentedsys.argv.remove('s')it should besys.argv.remove('-s')two conditions are contradicting each other:
Edited version of your code: