Search code examples
linuxpython-3.xargs

Python3 read args from file in Linux


I have program.py that accepts only one argument as URL:

python3 program.py http://www.xxx.xxx

and the code is something like this

def Video(url)
    #do something
    #with that url string

def Main():
    parser = argparse.ArgumentParser()
    parser.add_argument("url", help="Add URL", type=str)
    args = parser.parse_args()  
    Video(args.url) # here most the list of urls from file

if __name__ == '__main__':
    Main()

But I would like my program to read the URL from a file, one after the other has ended

Something like this:

Python3 program.py < URLfile

Thanks


Solution

  • done

        parser = argparse.ArgumentParser()
        parser.add_argument('url', nargs='+', type=str)
        args = vars(parser.parse_args())
    
        for x in args['url']:
            Video(x)
    
    if __name__ == '__main__':
        Main()
    

    and to use it:

     python3 program.py  $(<links)