Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多次执行python脚本_Python_Python 2.7 - Fatal编程技术网

多次执行python脚本

多次执行python脚本,python,python-2.7,Python,Python 2.7,我不确定这样做的最佳方式,但我有一个保存为.py的python脚本。此脚本的最终输出是两个文件x1.txt和y1.txt 基本上,我想运行这个脚本1000次,每次运行都用新名称编写两个文本文件,即x1.txt+y1.txt,然后第二次运行x2.txt和y2.txt 考虑到这一点,似乎最好从以下内容开始整个脚本 runs=xrange(:999) for i in runs: ##run the script 然后做一些有意义的事情 for i in runs: fil

我不确定这样做的最佳方式,但我有一个保存为.py的python脚本。此脚本的最终输出是两个文件x1.txt和y1.txt

基本上,我想运行这个脚本1000次,每次运行都用新名称编写两个文本文件,即x1.txt+y1.txt,然后第二次运行x2.txt和y2.txt

考虑到这一点,似乎最好从以下内容开始整个脚本

runs=xrange(:999)
for i in runs:

##run the script
然后做一些有意义的事情

    for i in runs:
        filnameA=prefix += "a"+i


open("filnamea.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))


    for i in runs:
        filnameB=prefix += "a"+i


open("filnameB.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))
这真的是最好的方法吗?我打赌这不是更好的主意


我知道你可以
导入时间
并编写一个与时间匹配的文件名,但这对以后的处理来说会很烦人。

如果你的计算机有资源并行运行这些文件,你可以使用
多处理
来完成。否则,使用循环按顺序执行它们

你的问题不是很明确,你被困在哪一部分。您是否只需要关于是否应该使用循环的建议?如果是,我的答案在上面。或者您还需要帮助来形成文件名吗?你可以这样做:

import sys

def myscript(iteration_number):
    xfile_name = "x%d.txt" % iteration_number
    yfile_name = "y%d.txt" % iteration_number
    with open(xfile_name, "w") as xf:
        with open(yfile_name, "w") as yf:
            ... whatever your script does goes here

def main(unused_command_line_args):
    for i in xrange(1000):
        myscript(i)
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv))

我不确定,但也许它可以帮助: 假设,我想打印“hello”10次,而不需要手动写10次。为此,我可以定义一个函数:

    #Function for printing hello 10 times:
    def func(x):
       x="hello"
       i=1
       while i<10 :
           print(x)
           i += 1
       else :
           print(x)

    
    print(func(1))
#打印hello 10次的功能:
def func(x):
x=“你好”
i=1

当您运行脚本1000次时,您会将结果写入2个文件中的每个文件1000次吗?在开始下一次运行之前,每个运行是否应该写入自己的输出文件?每个运行写入两个文件。我明白你的意思。