Python 如何在os.system()中指定路径以在Flask中执行shell脚本
我正在使用Flask执行一个shell脚本,下面是我的实际代码:Python 如何在os.system()中指定路径以在Flask中执行shell脚本,python,shell,flask,Python,Shell,Flask,我正在使用Flask执行一个shell脚本,下面是我的实际代码: def execute(cmd, files): os.system(cmd) back =dict() for file in files: with open(file, 'r') as f: info = f.read() back[file] = info return back @app.route('/executeScr
def execute(cmd, files):
os.system(cmd)
back =dict()
for file in files:
with open(file, 'r') as f:
info = f.read()
back[file] = info
return back
@app.route('/executeScript', methods = ['POST'])
def executeScript():
output = execute('./script.sh', ['file1.txt', 'file2.txt'])
return render_template('template.html', output=output)
但是我想把我的脚本(script.sh)放在一个特定的文件夹中。为此,我需要在我的代码中添加路径,但当添加它时,它不再起作用。我试过这样的方法:
output = execute(['sh', 'path/to/myscript/script.sh'], ['path/to/myscript/file1.txt', 'path/to/myscript/file2.txt'])
但这不起作用,根本不执行脚本。
你知道怎么做吗?根据(我的重点)的描述: 在子shell中执行命令(字符串) 当你试着跑的时候
execute(['sh', 'path/to/myscript/script.sh'], ...)
…您最终将列表传递给os.system
。试一试
execute('sh path/to/myscript/script.sh', ...)
根据(重点矿)的描述: 在子shell中执行命令(字符串) 当你试着跑的时候
execute(['sh', 'path/to/myscript/script.sh'], ...)
…您最终将列表传递给os.system
。试一试
execute('sh path/to/myscript/script.sh', ...)
您确定您的文件路径正确吗?三重检查,因为这是经常出现的问题;o 我会有这样一个变量:
script_dir = 'path/to/myscript'
some command > home/path/to/file.txt
那就有
output = execute(['sh', os.path.join(script_dir, 'script.sh')], [os.path.join(script_dir, 'file1.txt'), os.path.join(script_dir, 'file2.txt'])
因为这会减少你打字的可能性。您甚至可能想要更进一步,并拥有:
script = os.path.join(script_dir, 'script.sh')
file_1 = os.path.join(script_dir, 'file1.txt')
等等。
然后:
如果您的路径发生变化,这将使阅读和编辑变得更容易
希望这能解决问题 您确定您的文件路径正确吗?三重检查,因为这是经常出现的问题;o 我会有这样一个变量:
script_dir = 'path/to/myscript'
some command > home/path/to/file.txt
那就有
output = execute(['sh', os.path.join(script_dir, 'script.sh')], [os.path.join(script_dir, 'file1.txt'), os.path.join(script_dir, 'file2.txt'])
因为这会减少你打字的可能性。您甚至可能想要更进一步,并拥有:
script = os.path.join(script_dir, 'script.sh')
file_1 = os.path.join(script_dir, 'file1.txt')
等等。
然后:
如果您的路径发生变化,这将使阅读和编辑变得更容易
希望这能解决问题 您还可以使用“subprocess.Popen(path)”,子流程模块的在线文档是。您还可以使用“subprocess.Popen(path)”,子流程模块的在线文档是。这样的东西不是更好吗?它使用子流程模块,而不是不推荐使用的os.system()
这样不是更好吗?它使用子流程模块,而不是不推荐使用的os.system() 我知道了! 在我执行的脚本(script.sh)中,我将一些命令的输出重定向到文本文件。 我需要在这些文本文件中添加路径绝对路径,如下所示:
script_dir = 'path/to/myscript'
some command > home/path/to/file.txt
但这段代码运行正常:output=execute(['sh',path/to/myscript/script.sh'],['path/to/myscript/file1.txt',path/to/myscript/file2.txt'])
谢谢大家的帮助 我想出来了!
在我执行的脚本(script.sh)中,我将一些命令的输出重定向到文本文件。
我需要在这些文本文件中添加路径绝对路径,如下所示:
script_dir = 'path/to/myscript'
some command > home/path/to/file.txt
但这段代码运行正常:output=execute(['sh',path/to/myscript/script.sh'],['path/to/myscript/file1.txt',path/to/myscript/file2.txt'])
谢谢大家的帮助 如果从根提供绝对路径,并以正斜杠开始路径,会怎么样?例如,
/home/username/scripts/shell_script.sh
我也尝试过,但也不起作用。如果从根目录提供绝对路径,并以正斜杠开始路径,会怎么样?例如,/home/username/scripts/shell_script.sh
我也试过了,但也没用。我现在看到了另一个答案,doh!应该已经阅读了os.system的文档,我现在看到的是另一个答案,doh!应该已经阅读了os.system的文档,当我尝试这样做时总是会出现错误:path/to/myscript/script.sh:Permission denied当你在正确的轨道上时;权限问题是无关的,但也是一个问题。但是您确实应该尝试从os.system()
迁移,正如我在另一篇评论中所概述的那样。下面是我在尝试时遇到的错误:path/to/myscript/script.sh:Permission denied当您在正确的轨道上时;权限问题是无关的,但也是一个问题。但是你真的应该尝试从os.system()
迁移,正如我在另一篇评论中所概述的那样。这应该更清楚,但是os.system()
的基本信息已经过时了,你应该更喜欢子流程
甚至在os.system()
文档中也得到了认可。巧合的是,这也会使您当前的代码更容易修复,尽管错误的详细信息在另一个无关的答案中。这应该更清楚,但基本的信息是,os.system()
已经过时,您应该更喜欢子流程
,即使在os.system()中也会得到认可
文档。巧合的是,这也会使您当前的代码更容易修复,尽管错误的细节在另一个不相关的答案中