shell - How can I take a variable number of files as input for a python script? -
for illustration want able run hypothetical command:
$ python script.py *.txt option1 option2 and have execute on every file matches *.txt
currently have found info on operating on 1 file @ time
from sys import argv self, file, option1, option2 = argv perform_operation(file, option1, option2) #function definition
argv list. let's assume going pass filename arguments. if it's more complicated, go deets' answer.
self = sys.argv[0] arguments = sys.argv[1:] now, arguments list of programme arguments. let's want process them 1 @ time:
for argument in arguments: work(argument) or want pass of them function:
work(arguments) as passing *.txt argument. shell (before programme runs) of work you.
if run, python program.py *.txt *.txt refers 3 text files, shell expand such programme see python program.py a.txt b.txt c.txt.
python shell python-2.7 command-line command-line-arguments
No comments:
Post a Comment