-2

I want to take multiple file as input from command-line like

#python script.py file1.txt file2.txt file3.txt .... file_N.txt

following program can take only one file as input

python script.py file1.txt

But I want to take multiple file as input.

    import sys
with open(sys.argv[1], 'r') as file:
    wordcount = file.read()
    words= wordcount.split()
    #print(words)
    count = {}
    for word in words:
        if word in count:
            count[word]=count[word] + 1
        else:
            count[word] = 1
    print(count)
3

2 Answers 2

0

They are stored in sys.argv. They are stored in array and you can access them through index.

a = str(sys.argv)
// a[0]: file1.txt
// ...
Sign up to request clarification or add additional context in comments.

Comments

0

The sys module already contains all the arguments inside the sys.argv which is a list of all arguments passed to the program

The minimum working example I can come up with would be

import sys
names = sys.argv

This code generates a list of all arguments passed to the program. You could of course filter the input to make sure the names are valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.