如何使python循环可以运行一个程序很多次?

2024-05-17 15:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,我可以从终端使用,但我想运行它在一个循环在python很多次。有人叫我用子流程调用但我在理解它的工作原理上有些困难。你知道吗

我通常从终端运行./grezza_foresta -w "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.g" -m 5 -e 0 > file_name.g(使用-w-m-eare选项,>;创建一个带有输出的文件)

所以我试着按照别人告诉我的去做。你知道吗

import subprocess
subprocess.call(["g++", "/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta",  "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.g"])
ntrial = input("How many trials? ")
for i in range(int(ntrial)):
    tmp=subprocess.call("/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta")
    print(i,tmp)

我得到这个错误:

ld: can't link with a main executable file '/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta' for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

它实际上似乎在某种程度上起作用,但我不知道如何添加选项。你知道吗


Tags: text终端选项callusersfilesubprocessdesktop
1条回答
网友
1楼 · 发布于 2024-05-17 15:27:45

需要添加^ {< CD1>}标志并更改C++文件后缀:

import subprocess
subprocess.call(["g++", "-o", "/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta",  "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.cpp"])
ntrial = input("How many trials? ")
for i in range(int(ntrial)):
    tmp=subprocess.call("/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta")
    print(i,tmp)

相关问题 更多 >