开发者

Handling arbitrary number of command line arguments in python

I am trying to have my script be able to take in an arbitrary number of file names as command lin开发者_如何学JAVAe arguments. In Unix, it is possible to use the '*' key to represent any character. For example,

ls blah*.txt 

will list every file with blah at the beginning and txt at the end.

I need something like this for my python script.

python myscript.py blah*.txt

Is this possible? and if it is, how can it be done?


import sys

for arg in sys.argv[1:]:
  print arg

In Unix-land, the shell does the job of glob-expanding the commandline arguments, so you don't need to do it yourself. If you're processing a bunch of files in sequence, you might also look at the fileinput module, which works like Perl's "magic ARGV" handle and the -n and -i flags. It lets you loop over every line of every file named on the commandline, optionally moving the file to a backup name and opening stdout to the original name of the file, which lets you do something simple like:

import fileinput

for line in fileinput.input(inplace = True, backup = '.bak'):
  print fileinput.filelineno() + ": " + line

to add the line number to the beginning of every line of every file on the commandline, while saving the originals as filename.bak.


Thats why you have sys.argv (and dont forget to import sys) It will return all your blah*.txt as a list of filenames


I think you want to use glob.


There is also fnmatch for when you don't want to have to worry about the path.


import sys, glob
files = reduce(lambda x, y: x + y, (glob.glob(x) for x in sys.argv[1:]))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜