开发者

How to pass multiple arguments in command line using python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

(My Code):

import csv
import re
import string
import sys
import fileinput
import os
import random
import glob
import getopt

def getSymbols(filename):
    f = file(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print line, 
    f.close()
if len(sys.argv) < 2:
    print 'No action specified.'
    sys.exit()

    elif:
        print "No option"
    sys.exit()

    else:
        for filename in sys.argv[1]:
            readfi开发者_开发知识库le(filename)

    with open(filename) as f:
         for line in f:
             if 'symbols' in line:
                print "Total Number of Symbols:\n",line.strip(' has ');
getSymbols(filename)

I have a requirement and I am not able to find a solution:

How can I pass multiple file path as arguments in the command line ? Eg:

test.py C:\test1.txt C:\test2.txt

I have used the code as mentioned above to pass the file path as an argument and filename is the reference to that argument that I pass and it should print the total number of symbols. Please send me the sample code related to this issue.


The files are at argv[1], argv[2], and so on. So just take a slice.

for filename in sys.argv[1:]:
    readfile(filename)


you can use len(sys.argv) to count number of arguments and use them as sys.argv[1], sys.argv[2] etc.


You may find that the fileinput module is what you want for your program.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜