Skip to main content
 首页 » 编程设计

python之在 for 循环中使用 python 脚本

2025年05月04日21kuangbin

我想在 for 循环中插入一个 python 脚本(它只是打印出输入)。问题:代替将“i”作为变量——它只打印“i”,而不是“string1”、“string2”。 我试过:

for i in ['string1, string2']: 
    os.system("python scriptWhichPrintsInput.py i") 
... 
i 

请您参考如下方法:

您应该使用格式或字符串连接:

os.system("python scriptWhichPrintsInput.py " + i) 

或者:

os.system("python scriptWhichPrintsInput.py {}".format(i)) 

请参阅 Python 2 的文档和 Python 3