简体   繁体   中英

How to make a python loop that would run a program many times?

I have a program that I can use from the terminal but i would like to run it many times in a loop in python. Someone told me to use the subprocess.call function but I have some trouble understanding how that works.

From the terminal I usually run exactly this ./grezza_foresta -w "/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.g" -m 5 -e 0 > file_name.g (the -w -m -e are option and the > is to create a file with the output)

So I tried something like that from what I've been told to do .

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)

I'm getting this error :

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)

It actually seems to be working in some way but I don't know how to add the options.

You need to add the -o flag and change your C++ file suffix:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM