从另一个python程序在循环中调用一个python程序,并将失败的id号添加到列表中

从另一个python程序在循环中调用一个python程序,并将失败的id号添加到列表中,python,Python,我有一个Python程序,并接受第一个参数ID号 test1.py: import sys try: id=sys.argv[1] id=int(id) print(id) except: print("Pass ID number to Program") if (id!=3): print("Success") else: print("Failed 3 as exp")

我有一个Python程序,并接受第一个参数ID号

test1.py:

import sys

try:
    id=sys.argv[1]
    id=int(id)
    print(id)
except:
    print("Pass ID number to Program")
    
if (id!=3):
    print("Success")
else:
    print("Failed 3 as exp")
    exit()
 
我没有ID号,因此希望在循环中运行此程序。如果len(list2)>1,我想在下一次运行中为失败的ID号运行test1.py程序

请注意:test1.py将在3到4个案例中失败,我在这里给出了一个场景

test2.py:

import subprocess

list1=[1,2,3,4,5,6,7,8,9,10]
list2 = []

for i in list1:
    try:
        proc = subprocess.Popen("python test1.py i",stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        print(proc.communicate[0])
    except:
        list2.append(i)
        print(list2)
  • 子流程中的i值未替换
  • test1.py是失败ID号的出口,我不确定如何将其附加到列表2中。如果我在test1.py中追加,那么它将覆盖下一个失败的ID值。因为我想再次为失败的ID号运行程序(test1.py),所以在完成list1的循环之后

  • 您没有传递程序的ID号:

    proc = subprocess.Popen("python test1.py i", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    始终运行:python test1.py i

    例如,您需要:

    proc = subprocess.Popen(f"python test1.py {i}", stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    这就是将变量放入字符串的方式:

    >>> i = 3
    >>> f"python test1.py {i}"
    'python test1.py 3'
    

    它是
    Popen()
    。为什么不能
    导入test1
    ,然后从中调用一个函数?对不起,键入有问题的Popen和已编辑的问题。test1.py中有200多行代码,这里是示例一。似乎我的子流程。Popen调用是错误的,生成的输出如下:[1][1,2][1,2][1,2,3][1,2,3,4,5][1,2,3,4,5,6][1,2,3,4,5,6,7][1,2,3,4,5,6,8][1,2,3,4,5,6,7,9][1,2,3,3,3,4,4,4,4,4][1,4][1,2,4,4,4,4][1,4][1,2,2,4,4][2,4][1,4][1,4][