[英]why does this python script not work?
my python script is the following code: 我的python脚本是以下代码:
1 import subprocess
2
3 # initial 'output' to make
4 r0 = 21
5 # number of runs to make
6 rn = 10
7
8 rf = r0+rn
9
10 for i in range(r0, rf):
11 #output directory
12 opt_dir = 'output'+str(i)
13 #put it in this output directory
14 popt_dir = './output'+str(i)
15
16 subprocess.call(['mkdir', opt_dir])
17 subprocess.call(['./exp_fit', 'efit.inp'])
18 subprocess.call(['mv', 'at*', popt_dir])
the intention is this: 目的是这样的:
i have a program called "exp_fit" which takes an input file "efit.inp". 我有一个名为“ exp_fit”的程序,该程序需要一个输入文件“ efit.inp”。 one call to
./exp_fit efit.inp
will create output files called 'at0_l0_l0', 'at0_l1_l-1', ... etc (total 475 files starting with 'at'). 对
./exp_fit efit.inp
一次调用将创建名为'at0_l0_l0','at0_l1_l-1'等的输出文件(总共475个文件以'at'开头)。
now, i have been generating data files by running 'exp_fit', then creating output directories and moving them into the output directories with the following bash commands: (for example, with the 20th run of my code) 现在,我一直在通过运行'exp_fit'生成数据文件,然后使用以下bash命令创建输出目录并将其移至输出目录:(例如,使用我的代码的第20次运行)
mkdir output20
mv at* ./output20
so i would think that my script should do the same thing. 所以我认为我的脚本应该做同样的事情。 however, it only does the following:
但是,它仅执行以下操作:
(1) it correctly generates all output files (475 files starting with 'at') (2) it correctly creates the desired directories (output21 - output30) (3) it DOES NOT, however, correctly move all the output files starting with 'at' into the desired directories. (1)正确生成所有输出文件(以'at'开头的475个文件)(2)正确创建所需目录(output21-output30)(3)不会,但是,正确移动所有以'开头的文件在”进入所需目录。 why is this?
为什么是这样? shouldn't the call to line 18 correctly execute the command to move all my files starting with 'at' into the desired directory?
第18行的调用是否应该正确执行命令以将我所有以'at'开头的文件移动到所需目录中?
should i be writing this script with bash instead of python? 我应该用bash而不是python编写此脚本吗? what is wrong with this?
这有什么问题?
Don't issue subprocess
calls for things you can do natively from Python. 不要为您可以从Python本地完成的事情发出
subprocess
调用。 To move files/dirs around, just use os.rename
. 要移动文件/目录,只需使用
os.rename
。
To create a directory, use os.mkdir
. 要创建目录,请使用
os.mkdir
。
To execute an external program, using subprocess
is the right tool. 要执行外部程序,使用
subprocess
是正确的工具。
The problem is that this subprocess command 问题是此子流程命令
subprocess.call(['mv', 'at*', './output20'])
is not the same as typing this at a prompt 与在提示符下键入此命令不同
$ mv at* ./output20
In the latter case, the bash glob expansion converts the single at*
argument to a list of arguments of matching filenames for the mv
command. 在后一种情况下,bash glob扩展将单个
at*
参数转换为与mv
命令匹配的文件名的参数列表。 So the kernel sees the second as 因此内核将第二个视为
['mv', 'at0_l0_l0', 'at0_l1_l-1', './output20']
kev's answer tells Python to pass the command through the shell, so the escaping will occur. kev的答案告诉Python将命令通过外壳传递,因此会发生转义。
But the better solution is to use the glob
module and os.rename
libraries and not call the subprocess. 但是更好的解决方案是使用
glob
模块和os.rename
库,而不调用子os.rename
。 Creating subprocesses is expensive, and using shell=True
could lead to security holes, so it's best to avoid that habit. 创建子流程非常昂贵,并且使用
shell=True
可能会导致安全漏洞,因此最好避免这种习惯。
(Actually, I suggest making the output directory, switching into it, and then running the exp_fit
program from within that directory. Then you won't have to move the output. Try that first.) (实际上,我建议创建输出目录,切换到该目录,然后从该目录中运行
exp_fit
程序。然后,您不必移动输出。请首先尝试。)
If shell=True
, the executable argument specifies which shell to use. 如果
shell=True
,则可执行参数指定要使用的外壳。
On Unix, the default shell is /bin/sh
. 在Unix上,默认shell是
/bin/sh
。
subprocess.call(['mv', 'at*', popt_dir], shell=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.