2017-09-25 4 views
0

Je program.py qui accepte un seul argument URL:python3 lire args de fichier sous Linux

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

et le code est quelque chose comme ça

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() 

Mais je voudrais que mon programme lire l'URL d'un fichier, l'un après l'autre a terminé

Quelque chose comme ceci:

Python3 program.py < URLfile 

Merci

Répondre

0

fait

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() 

et à l'utiliser:

python3 program.py $(<links) 
0
import sys 
for url in sys.stdin.readlines(): 
    do_something(url.strip()) 
+0

Je voudrais être plus d'une ligne de texte dans le URLfile, eh oui –

+0

juste vu cela et a changé ma réponse –

0

Je pense configparser est ce que vous avez besoin.
Format flexible:

[DEFAULT] 
ServerAliveInterval = 45 
Compression = yes 
CompressionLevel = 9 
ForwardX11 = yes 

[bitbucket.org] 
User = hg