简体   繁体   English

python 脚本调用另一个脚本

[英]python script calling another script

I wrote a python script that works.我写了一个有效的 python 脚本。 The first line of my script is reading an hdf5 file我脚本的第一行是读取 hdf5 文件

readFile = h5py.File('FileName_00','r')

After reading the file, my script does several mathematical operations, successfully working.阅读文件后,我的脚本进行了几次数学运算,成功运行。 In the output I got function F.在 output 中,我得到了 function F。

Now, I want to repeat the same script for different files.现在,我想为不同的文件重复相同的脚本。 Basically, I only need to modify FileName_00 by FimeName_01 or....FileName_10.基本上,我只需要通过FimeName_01 或....FileName_10 来修改FileName_00。 I was thinking to create a script that call this script!我正在考虑创建一个调用此脚本的脚本!

I never wrote a script that call another script, so any advice would be appreciable.我从来没有写过调用另一个脚本的脚本,所以任何建议都是可以理解的。

One option: turn your existing code into a function which takes a filename as an argument:一种选择:将现有代码转换为 function ,它将文件名作为参数:

def myfunc(filename):
  h5py.file(filename, 'r')
  ...

Now, after your existing code, call your function with the filenames you want to input:现在,在您现有的代码之后,使用您要输入的文件名调用您的 function:

myfunc('Filename_00')   
myfunc('Filename_01')
myfunc('Filename_02')
...

Even more usefully, I definitely recommend looking into更有用的是,我绝对建议研究

if(__name__ == '__main__')

and argparse ( https://docs.python.org/3/library/argparse.html ) as jkr noted.argparse ( https://docs.python.org/3/library/argparse.html ) 如 jkr 所述。

Also, if you put your algorithm in a function like this, you can import it and use it in another Python script.此外,如果您将您的算法放在像这样的 function 中,您可以导入它并在另一个 Python 脚本中使用它。 Very useful!很有用!

Although there are certainly many ways to achieve what you want without multiple python scripts, as other answerers have shown, here's how you could do it.虽然肯定有很多方法可以在没有多个 python 脚本的情况下实现您想要的,但正如其他回答者所展示的那样,您可以这样做。

In python we have this function os.system (learn more about it here: https://docs.python.org/3/library/os.html#os.system ). In python we have this function os.system (learn more about it here: https://docs.python.org/3/library/os.html#os.system ). Simply put, you can use it like this:简单地说,你可以像这样使用它:

os.system("INSERT COMMAND HERE")

Replacing INSERT COMMAND HERE with the command you use to run your python script.INSERT COMMAND HERE替换为您用于运行 python 脚本的命令。 For example, with a script named script.py you could conceivably (depending on your environment) include the following line of code in a secondary python script:例如,使用名为script.py的脚本,您可以想象(取决于您的环境)在辅助 python 脚本中包含以下代码行:

os.system("python script.py")

Running the secondary python script would run script.py as well.运行辅助 python 脚本也将运行script.py FWIW, I don't necessarily think this is the best way to accomplish your goal -- I tend to agree with DraftyHat's solution in most circumstances. FWIW,我不一定认为这是实现您目标的最佳方式——在大多数情况下,我倾向于同意 DraftyHat 的解决方案。 But in case you were curious, this is certainly an option in python.但如果您好奇,这肯定是 python 中的一个选项。 I've used this functionality in the past, albeit not to run other python scripts, but to execute commands in the shell.我过去使用过这个功能,虽然不是运行其他 python 脚本,而是执行 shell 中的命令。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM