简体   繁体   English

为什么这个python脚本不起作用?

[英]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.

相关问题 为什么这个脚本不适用于线程化python - why does this script not work with threading python 为什么deleteUI在使用此脚本的Maya Python中不起作用? - Why does deleteUI not work in Maya Python with this script? 为什么Python脚本适用于Windows而不适用于Linux? - Why does a Python script work on Windows and not in Linux? 为什么这个python脚本只能在Ubuntu 12.04上使用IDLE? - Why does this python script only work from IDLE on Ubuntu 12.04? 为什么我的Python代码在Jupyter Notebook中工作,而不是作为脚本? - Why does my Python code work in Jupyter Notebook but not as a script? 为什么这个 python 脚本可以在我的本地机器上运行,而不能在 Heroku 上运行? - Why does this python script work on my local machine but not on Heroku? 为什么这个非常简单的Python脚本不起作用? 虽然不循环 - Why does this very simply piece of Python script not work? While not loop 为什么我的 python 脚本在 temal 和 cron 之间的工作方式不同? - Why does my python script work differently between the teminal and cron? 为什么单个 Python 脚本中的两个主要函数可以工作? - Why does two main functions in a single Python script work? 为什么这不起作用??? 它在python上: - why does this not work??? It is on python:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM