1

I have a function which takes one compulsory file and other possible optional file. Currently the function can read and write single file from the user input. But I want to load and write as many files as user enters from the console input. For that, I want to define function for one compulsory filename and other as an optional parameters.

How do I ask users in console input to enter one compulsory file and other all possible optional filename (only if user wants) and read and write them separately in a function without mixing each other. I want to read and also write separately those all entered files.

Basically, I want to load all filenames which were entered by users in console input and write them separately in each new file.

Currently my function loads and read only one file from the user input.

def read_files(filename, *other):
    with open(filename, 'rU') as f:
        d = json.load(f)

Input for users:

if __name__ == '__main__':
    filename = input('Enter your file name:')

    #Here I am confused how do I ask the possible number of filename and How 
    #do i stop when the user enters all filename
    other = input('Enter your other file name')
    read_files(filename, )
2
  • 1
    I would recommend you look into the 'argparse' module, as well. You could add an argument to your script that could take 1 or more names, then run it like 'python myscript.py name1.txt name2.txt'. Commented Jun 26, 2018 at 21:09
  • If you do not need option parsing, it might be sufficient to use sys.argv instead of argparse. Commented Jun 26, 2018 at 22:19

1 Answer 1

1

You could hint that q for quit leads to stop adding further filenames:

if __name__ == '__main__':
    filename = input('Enter your file name:')

    other=[]
    while True:
        inp  = input('Enter your other file name (q for quit)')
        if inp == 'q':
            break
        else:
            other.append(inp)
    read_files(filename, other)

EDIT: Probably it's even more convenient to stop if nothing was entered, so the while loop would be:

    while True:
        inp  = input('Enter your other file name (press ENTER for quit)')
        if inp == '':
            break
        else:
            other.append(inp)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but how do i read and write all those possible optional files separately inside the function based on the number of files it was entered in the console ?
That's a bit of a separate question. Now that you have an array, called "other", you will iterate over each file in that array, and open each one. Another for loop, and you'll call "read_file(name)" each time, is how I would do it. Something like: for name in input_files: read_contents(name) (sorry about the formatting). Here is a different question you might want to look into: stackoverflow.com/questions/16095855/…

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.