Read a list of file names without typing them manually
Question: I have a python code inside which the name of file is received with input
command

fileName = input("please enter the file name")

My purpose is to write a script to run this file. But I do not want to sit
there and input each file name manually. Is it possible to give the fileName
as a command line argument or so while running.

Edit: More explanation: I am given python codes with the above line inside,
which I should run on some sample input. I do not want to change the file or
sample input only want to run the file on the input automatically. The reason
is that there are many files to be checked and many sample inputs.


Answer: You have a few options, really.

# Redirect stdin to a file

One you might not have considered is leaving your program the way it is,
creating a text file with a list of file names, and then redirecting the
standard input to the file. Let's name the file `myfilelist.txt`. The file
would just be a list:

    
    
    ~/file1.txt
    ~/file2.txt
    

Or on Windows:

    
    
    C:\file1.txt
    C:\file2.txt
    

Then you would call your program like so:

    
    
    python ./myprogram.py < ./myfilelist.txt
    

Here, the `<` character tells the operating system to use the contents of
`myfilelist.txt` instead of waiting for you to type. This does assume that
your program loops and continues to prompt for multiple files.

If it only accepts one file at a time, then you might consider...

# Using a script

You can write a script that calls your program a bunch of times separately,
each time with a different file name. A bash script on Linux might look like
this:

    
    
    #!/bin/bash
    echo '~/file1.txt' | python ./myprogram.py
    echo '~/file2.txt' | python ./myprogram.py
    

Or a batch file on Windows:

    
    
    echo C:\file1.txt | python .\myprogram.py
    echo C:\file2.txt | python .\myprogram.py
    

Here, we execute the command `echo` to make the system "print" out some text,
and then we use the `|` (pipe) to tell the system to use `echo`'s output as
the other program's input (instead of printing the text to the screen).

# List file argument

Another option is to create an argument to your program that accepts a
_single_ file path, and then use the contents of that file as a list of files
to process. This requires modifying your program.

A quick and dirty way of doing that in code:

    
    
    import sys
    if '__main__' == __name__:
        list_file = sys.argv[1]
        with open(list_file) as f:
            for r in f:
                do_my_other_code(r)
    

Then you'd call it like this:

    
    
    python ./myprogram.py ./myfilelist.txt
    

Look into [`argparse`](https://docs.python.org/3.3/library/argparse.html),
[`getopt`](https://docs.python.org/2/library/getopt.html), or similar to make
this cleaner.

# Argument list

You could just list all the files as part of the original command. Your code
would look something like this:

    
    
    import sys
    if '__main__' == __name__:
        for a in sys.argv[1:]:
            do_my_other_code(a)
    

(Note that `sys.argv[1:]` is what's called a "slice". Look that up if you're
unfamiliar with them.)

Then you would call it like this:

    
    
    python ./myprogram.py ~/file1.txt ~/file2.txt
    

I don't especially like this option for your case because your question
suggests you have a fairly large list of files. Typing the command would be
tedious and error prone. You also wouldn't have the list saved anywhere if the
computer crashed or something. But it'd be fine for just a few files.

# Other

There are lots of ways to do things like this. These are just a few fairly
simple options. The _best_ one will depend on your exact usage.

