Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一个接一个地运行许多python脚本_Python_Linux_Bash_Operating System - Fatal编程技术网

一个接一个地运行许多python脚本

一个接一个地运行许多python脚本,python,linux,bash,operating-system,Python,Linux,Bash,Operating System,专家们,我有50个python脚本,我想在同一个文件夹中逐个运行。但问题是,在我工作的系统中,许多人也经常使用它。因此有可能滥用我的python脚本。因此,我想将50个python脚本复制到同一个文件夹中,并删除python脚本(script1.py,script2.py…)在运行主脚本(包含所有正在运行的脚本)之后从同一文件夹中删除 #!/bin/sh python script1.py python script2.py python script3.py python script4.py

专家们,我有50个python脚本,我想在同一个文件夹中逐个运行。但问题是,在我工作的系统中,许多人也经常使用它。因此有可能滥用我的python脚本。因此,我想将50个python脚本复制到同一个文件夹中,并删除python脚本(script1.py,script2.py…)在运行主脚本(包含所有正在运行的脚本)之后从同一文件夹中删除

#!/bin/sh
python script1.py
python script2.py
python script3.py
python script4.py
python script5.py
python script6.py
python script7.py
python script8.py
python script9.py
python script10.py
....

但是在上面的脚本中,问题是如果我删除python脚本,只有第一个python脚本,即
python script1.py
正在运行,而不是其他脚本。我希望专家能帮助我克服这个问题。

你做得不对

您应该编写一个main.py程序,它将一个接一个地执行您的函数

在当前使用的方式上,当cli传递到script1.py时,只执行第一个脚本,如果脚本不退出,则忽略其他脚本

例如,如果你犯了一个无限循环错误,那么yeah other将永远不会运行

我建议学习python导入和执行

一种方法是:

# main.py you have the script1.py and others in same folder.
# you need to have the scripts inside of the functions so you could as well do that in same file.
# so script1 should look like:
def script1_func(): # any arguments that are needed can be passed
    # code of your script here
    return value # if your script returns some value write it there

# then in main.py
import script1
import script2
import script3

if __name__ == '__main__':
    script1.script1_func()
    script2.script2_func()
    script3.script3_func()

# on that way you can execute them all in sequence. If needed to use paralelism check multithreading.
# if you need to remove them or any other files.
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# this will give you a path to the file and then you just issue delete
if os.path.exists(basedir + "script1.py"):
    os.remove(basedir + "script1.py")
else:
  print("The file does not exist")
os.rmdir(basedir) # this will delete whole folder
顺便说一句,最好使用更好的约定,不要重复类似的功能

另一种方法是在bash中的每个文件上运行exec,如果脚本多于内核,这可能会导致机器负载过大

这是另一个答案。
所以其他用户建议您使用find命令并行执行脚本。

为了满足您的确切需要,您可以使用下面的解决方案

我创建了两个示例python文件和一个shell脚本来调用这两个脚本

➜  65976750升
sandex.sh script1.py script2.py
调试模式执行
➜  65976750 bash-x sandex.sh
+mkdir./sandexec
+查找。-name'*.py'-exec cp'{}./sandexec';'
+python3./sandexec/script1.py
运行python脚本1
+python3./sandexec/script2.py
从script2运行
+rm-rf./sandexec
原件
请让它更容易些它有点让人困惑➜ 65976750这是什么?如果我从同一个文件夹中删除python脚本,它会起作用吗?如果它不起作用,它会删除同一个目录,那么我在哪里保存输出?请提供一个示例脚本,我是否应该在main中包含所有python脚本。py是的,我会链接你示例链接在这里..你可以建议吗
➜  65976750 cat sandex.sh
mkdir ./sandexec
find . -name "*.py" -exec cp {} ./sandexec \;
python3 ./sandexec/script1.py
python3 ./sandexec/script2.py
rm -rf "./sandexec"

➜  65976750 ls
sandex.sh  script1.py script2.py
➜  65976750