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, )
user96564
  • 1,578
  • 5
  • 24
  • 42
  • 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'. – supermitch Jun 26 '18 at 21:09
  • If you do not need option parsing, it might be sufficient to use `sys.argv` instead of `argparse`. – Jan Christoph Terasa Jun 26 '18 at 22:19

1 Answers1

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)
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • 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 ? – user96564 Jun 26 '18 at 21:13
  • 2
    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: https://stackoverflow.com/questions/16095855/whats-the-most-pythonic-way-to-iterate-over-all-the-lines-of-multiple-files – supermitch Jun 26 '18 at 21:16