我试图通过python脚本启动一个可执行文件,它提供并循环不同的参数。
可执行文件将一系列文件保存到磁盘,所有文件都具有相同的名称。我想使用python脚本根据我提供的命令将不同的输出保存在不同的文件夹中。我的python脚本如下所示:
#! /usr/bin/env python
import subprocess
import csv
import sys
if len(sys.argv) != 2:
print 'Give a number for the folder name as an argument'
quit()
loop_params = [i*0.001 for i in range(100,270,10)]
param1 = 0.0001
param2 = 0.0005
param3 = 0.25
param4 = 0.92
param5 = 0.66
param6 = 0.75
param7 = -1
param8 = 2
arg2 = str(param1)
arg3 = str(param2) #each arg is passed to the program
arg4 = str(param3)
arg5 = str(param4)
arg6 = str(param5)
arg7 = str(param6)
arg8 = str(param7)
arg9 = str(param8)
new_dir = 'Run'+str(sys.argv[1])
# Remove folder if there and then make it again
subprocess.call(['rm -r '+new_dir+' ; mkdir '+new_dir], shell=True)
for t in loop_params:
xdir = str(t)
cmd = './main' # executable
arg1 = str(t) #each arg is passed to the program
print cmd+" "+arg1+" "+arg2+" "+arg3+" "+arg4+" "+arg5+" "+arg6+" "+arg7+" "+arg8+" "+arg9
p = subprocess.Popen([cmd,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
stor_dir = new_dir+'/'+xdir
#print out
subprocess.call(['mkdir '+stor_dir], shell=True)
subprocess.call(['mv *txt '+stor_dir],shell=True)
# Info on the paramters
this_run = "Loop over "+str(arg1)+"\n param1 = "+arg2+"\n param2 = "+arg3+"\n param3 = "+arg4+"\n param4 ="+arg5+"\n param5 ="+arg6+" \n param6 ="+arg7+"\n param7 = "+arg8+"\n param7 = "+arg8
ofile = open("this_run.info", "wb")
ofile.write(this_run)
ofile.close
subprocess.call(['mv *.info '+new_dir],shell=True)
这很好用(虽然它可能是一种非常快速和肮脏的做事方式):它运行程序循环遍历数组loop_params
创建文件夹new_dir
并创建所有子文件夹它,将文件移动到它们。
问题:如果我再次启动它,因为.\main
可执行文件通过附加到它们来编写自己的文件而python脚本稍后才将它们移动到最终文件夹,它们会被覆盖并搞砸。
我想做点什么
mkdir new_dir
loop ./new_dir/main
[... other loop operations ...]
以便在new_dir
中创建文件,并且不会覆盖。我试图在new_dir
中复制可执行文件(ok),但如果我使用./new_dir/main
,程序总是在启动python脚本的当前目录中输出。我还尝试向cwd = new_dir
提供subprocess.Popen
选项,但它不起作用。
我是python的新手,我正在尝试调整项目中其他人的脚本以满足我的需求。
谢谢
答案 0 :(得分:0)
这是我的版本。主要是简单的改进,比如删除重复的str()
调用,字符串格式化和一些优化。删除外部程序调用可能会对性能产生一些影响,但原则是避免创建不必要的子进程。毕竟,这是像Python这样使用shell的语言的主要优点之一。
还有其他可能的改进,但我不想走太远。
它没有做任何要求,因为我无法弄清楚还需要什么:
#! /usr/bin/env python
import subprocess
import csv
import sys
# These added
import os
import shutil
import glob
if len(sys.argv) != 2:
# Write error messages to stderr
print >>sys.stderr, 'Give a number for the folder name as an argument'
quit()
# Why not make these strings in the first place? Also might be easier as a list
param1 = 0.0001
param2 = 0.0005
param3 = 0.25
param4 = 0.92
param5 = 0.66
param6 = 0.75
param7 = -1
param8 = 2
arg2 = str(param1)
arg3 = str(param2) #each arg is passed to the program
arg4 = str(param3)
arg5 = str(param4)
arg6 = str(param5)
arg7 = str(param6)
arg8 = str(param7)
arg9 = str(param8)
# sys.argv is a list of strings
new_dir = 'Run' + sys.argv[1]
# Remove folder if there and then make it again
# No need to use a shell or the rm and mkdir programs
#subprocess.call(['rm -r '+new_dir+' ; mkdir '+new_dir], shell=True)
shutil.rmtree(new_dir, True)
os.mkdir(new_dir)
os.chdir(new_dir)
cmd = './main' # executable, removed outside the loop
# This was 'loop_params' and 't'
for xdir in [str(i*0.001) for i in xrange(100,270,10)]:
arg1 = xdir # each arg is passed to the program
print "%s %s %s %s %s %s %s %s %s %s " % (cmd,
arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
# This might be where we specify cwd, but might have to create the directory first
p = subprocess.Popen([cmd,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9])
# You are not using these, so why are you capturing them?
# stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# out, err = p.communicate()
p.wait()
stor_dir = new_dir + '/' + xdir
# No need to call 3 external programs (shell, mkdir, mv) for this
#subprocess.call(['mkdir '+stor_dir], shell=True)
#subprocess.call(['mv *txt '+stor_dir],shell=True)
os.mkdir(stor_dir)
for fname in glob.iglob('*txt'):
shutil.move(fname, stor_dir)
# Info on the parameters
this_run = """
Loop over %s
param1 = %s
param2 = %s
param3 = %s
param4 = %s
param5 = %s
param6 = %s
param7 = %s
param7 = %s
""" % (arg1, arg2 ,arg3 ,arg4, arg5, arg6, arg7, arg8, arg8)
# Are you sure this should be a binary file?
with open("this_run.info", "wb") as ofile:
ofile.write(this_run)
# No need to call 2 external programs (shell, and mv) for this
#subprocess.call(['mv *.info '+new_dir],shell=True)
for fname in glob.iglob('*.info'):
shutil.move(fname, new_dir)
如果您想再次移动文件,可以在此处完成。