![](/img/trans.png)
[英]Running Python Script from Bash - How to Redirect the Python Logging File
[英]running bash script from python file
我有一个bash脚本,它会在命令行中更改路径,这个,
#!/usr/bin/env python
cd /mnt/vvc/username/deployment/
我有一个python脚本,希望在路径更改为所需路径后运行,
剧本,
#!/usr/bin/env python
import subprocess
import os
subprocess.call(['/home/username/new_file.sh'])
for folder in os.listdir(''):
print ('deploy_predict'+' '+folder)
我明白了
File "/home/username/new_file.sh", line 2
cd /mnt/vvc/username/deployment/
^
SyntaxError: invalid syntax
有关如何解决此问题的任何建议吗?
您需要明确告诉子进程与哪个shell一起运行sh文件。 可能是以下之一:
subprocess.call(['sh', '/home/username/new_file.sh'])
subprocess.call(['bash', '/home/username/new_file.sh'])
但是,这不会更改python程序的工作目录,因为该命令在单独的上下文中运行。
您想要执行此操作以在运行时更改python程序的工作目录:
os.chdir('/mnt/vvc/username/deployment/')
但这并不是真正的好习惯。 将路径传递到os.listdir而不更改工作目录可能更好:
os.listdir('/mnt/vvc/username/deployment/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.