我正在尝试使用subprocess.Popen('cp‘等)在while循环中传输和重命名一些文件。并等待()。不幸的是,wait()命令似乎没有正常工作,即没有等待文件完全复制到新目录。然而,在大多数情况下,文件复制得很好,但有一小部分随机文件不能复制(每次运行脚本时不是相同的文件),因此是零字节文件或不完整文件。我也尝试过使用subprocess.check_call(),但也不起作用。当我打印poll()值时,它总是零,这应该意味着这个过程已经结束了。注意,我正在处理的所有文件都在150KB的范围内。由于我使用的是iraf例程,我的python脚本使用的是iraf (图像压缩和分析工具)的python版本python2.7。有没有办法强制Popen()或check_call()等待文件传输完成?
while count <= ncross_correlate and skip_flag != 's':
...more stuff
try:
iraf.rv.fxcor (object_fits, template_fits, apertures="*", cursor="",
continuum="both", filter="both", rebin="smallest", pixcorr="no",
osample=osample_reg, rsample=osample_reg, apodize=0.1, function="gaussian",
width="INDEF", height=0., peak="no", minwidth=3., maxwidth=21., weights=1.,
background=0., window=300., wincenter="INDEF", output=output_name, verbose="long",
imupdate="no", graphics="stdgraph", interactive="no", autowrite="yes",
autodraw="yes", ccftype="image", observatory="aao", continpars="", filtpars="",
keywpars="")
# Create a eps file of the cross_correlation file.
gki_output_name = output_name + '.gki'
iraf.plot.sgikern (gki_output_name, device='eps', generic='no', debug='no',
verbose='no', gkiunit='no')不幸的是,将在fxcor中创建的.gki文件转换为iraf之外的某种可读格式的唯一方法是调用iraf任务sgikern,该任务sgikern将一个.eps文件转储到我的iraf/iraf/目录中,而不提供更改文件名或目录位置的选项。事实上,文件名是随机生成的。非常令人沮丧!还要注意,使用iraf.plot.sgikern创建的任何eps文件都没有任何错误(即,开始时没有0KB文件)。复制和重命名是我遇到问题的地方。
# Find the eps file in /iraf/iraf/, rename it, and move to proper output location.
iraf_dir = '/iraf/iraf/'
eps_file_list = glob.glob(iraf_dir + 'sgi' + '*.eps')
...more code在这一点上,我已经尝试使用check_call()或Popen():
subprocess.check_call(['cp', eps_file_list[0], ccf_output_dir + object_name_sub +
'.eps'], stdout=subprocess.PIPE)
subprocess.check_call(['rm', eps_file_list[0]], stdout=subprocess.PIPE)或
process1 = subprocess.Popen(['cp', eps_file_list[0], ccf_output_dir +
object_name_sub + '.eps'], stdout=subprocess.PIPE)
process1.wait()
process2 = subprocess.Popen(['rm', eps_file_list[0]], stdout=subprocess.PIPE)
process2.wait()
...more stuff
# end of try statement
#end of while statement 我估计,如果我能以某种方式将两个Popen语句组合到一个Popen语句中,并包括大约0.01秒的shell休眠时间,以强制其他两个进程在返回一个完整的进程之前完成,那么就可能修复它。也许是这样的,尽管我不确定确切的发送:
process1 = subprocess.Popen(['cp', eps_file_list[0], ccf_output_dir +
object_name_sub + '.eps']; ['rm', eps_file_list[0]]; ['sleep', 0.01],
stdout=subprocess.PIPE)
process1.wait() 希望这能让你对我想要做的事情有所了解。我已经尝试了很多不同的东西,到处寻找这个问题的解决方案,但我真的被困住了。
干杯,布雷特
发布于 2013-01-22 15:51:14
也许以下内容就足够了:
subprocess.check_call(['mv', eps_file_list[0], ccf_output_dir + object_name_sub +
'.eps'], stdout=subprocess.PIPE)和
process1 = subprocess.Popen(['mv', eps_file_list[0], ccf_output_dir +
object_name_sub + '.eps'], stdout=subprocess.PIPE)
process1.wait()发布于 2013-01-22 15:55:59
您是否考虑过使用shutil.copyfile进行复制,使用os.remove进行删除?
如果你真的想使用子进程,我相信它的语法是这样的:
process1 = subprocess.Popen('cp ' + eps_file_list[0] + ' ' + ccf_output_dir +
object_name_sub + '.eps; rm ' + eps_file_list[0] ' + '; sleep 0.01',
stdout=subprocess.PIPE)这样,您调用的命令都在一个字符串中:'cp whatever foo/bar.eps; rm whatever; sleep 0.01'
您还可以使用三重引号设置字符串格式,并将命令放在单独的行上:
'''
cp %s %s%s
rm %s
sleep %s
''' % (eps_file_list[0], ccf_output_dir, object_name_sub, eps_file_list[0], 0.01)发布于 2013-02-05 09:18:36
这不是一个完整的解决方案,也不是一个令人满意的解决方案,但这是我想出的最好的解决方案,大约99.9%的时间有效(在我创建的4000+ eps中,有5个文件不是0字节就是不完整)。这比我原来做这件事的方式有了改进,原来我做这件事的方式有95%是成功的。我已经粘贴了以下代码:
try:
iraf.rv.fxcor (object_fits, template_fits, apertures="*", cursor="",
continuum="both", filter="both", rebin="smallest", pixcorr="no", osample=osample_reg,
rsample=osample_reg, apodize=0.1, function="gaussian", width="INDEF", height=0., peak="no",
minwidth=3., maxwidth=21., weights=1., background=0., window=300.,
wincenter="INDEF", output=output_name, verbose="long", imupdate="no",
graphics="stdgraph", interactive="no", autowrite="yes", autodraw="yes",
ccftype="image", observatory="aao", continpars="", filtpars="", keywpars="")
# Create a eps file of the cross_correlation file.
gki_output_name = output_name + '.gki'
iraf.plot.sgikern (gki_output_name, device='eps', generic='no', debug='no',
verbose='no', gkiunit='no')
time.sleep(0.25)当我发现在我的iraf目录中创建的一些ps文件在我的代码尝试将文件移动到另一个目录时还没有完全写入时,我在这里设置了一个时间休眠程序。
# Find the eps file in /iraf/iraf/, rename it, move to proper output location, and delete the old eps file.
iraf_dir = '/iraf/iraf/'
eps_file_list = glob.glob(iraf_dir + 'sgi' + '*.eps')
...continuation of code
if len(eps_file_list) == 1:
eps_file_sub = os.path.basename(eps_file_list[0])
cmd1 = 'cp {0} {1}'.format(eps_file_list[0], ccf_output_dir + object_name_sub + '.eps')
cmd2 = 'rm {0}'.format(eps_file_list[0])
cmd3 = 'sleep 0.05'
process1 = subprocess.Popen("{}; {}; {}".format(cmd1, cmd2, cmd3), shell=True, stdout=subprocess.PIPE)
process1.wait()对于process1,我发送了三个子进程外壳命令。第一种方法是将eps文件从我的/iraf目录复制到另一个目录(最初创建它们的iraf函数不允许我为这些文件提供适当的名称或输出位置)。第二种方法是从我的/iraf目录中删除eps文件。第三个命令是强制内核休眠。通过这样做,Python在达到睡眠命令之前不会收到已完成的信号。我相信这一部分工作得很完美。唯一的问题是,当我执行此命令时,用于创建eps文件的iraf例程很少能够足够快地创建它们。
#endif
num_cross = num_cross + 1
#Endtry
...more code这是一个非常笨拙的解决方案,一点也不令人满意,但它确实在99.9%的时间内有效。如果有人有更好的解决方案,请让我知道。这是一个非常令人沮丧的问题,我问过的每个人都想不出更好的办法(包括我的天文部门经常用python编程的人)。
https://stackoverflow.com/questions/14453622
复制相似问题