gpt4 book ai didi

linux - Python3 在 Linux 中从文件中读取参数

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:32 24 4
gpt4 key购买 nike

我的 program.py 只接受一个参数作为 URL:

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

代码是这样的

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

但我希望我的程序从文件中读取 URL,一个接一个地结束

像这样:

Python3 program.py < URLfile

谢谢

最佳答案

import sys
for url in sys.stdin.readlines():
do_something(url.strip())

关于linux - Python3 在 Linux 中从文件中读取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46415704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com