将 python 脚本中的变量解析为 shell
Parsing a variable inside a python script to shell
大家好,我是 python 的新手,我正在尝试研究如何将变量解析为 shell。
例如;
#!/usr/python3
import os
import subprocess
sometext = 'Hello World'
os.system("echo $sometext")
这 returns 没什么,我假设这不会正确解析。但是解释了我正在尝试做的事情。
我做了一些挖掘并尝试替换最后一行
#!/usr/python3
import os
import subprocess
sometext = 'Hello World'
subprocess.call('echo {sometext}')
这会出现以下错误;
Shell % python3 pingtest.py
Traceback (most recent call last):
File "pingtest.py", line 12, in <module>
subprocess.call('echo {sometext}')
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'echo {sometext}'
#!/usr/bin/python3
import subprocess
sometext = 'Hello World'
subprocess.call(["echo", sometext])
大家好,我是 python 的新手,我正在尝试研究如何将变量解析为 shell。 例如;
#!/usr/python3
import os
import subprocess
sometext = 'Hello World'
os.system("echo $sometext")
这 returns 没什么,我假设这不会正确解析。但是解释了我正在尝试做的事情。
我做了一些挖掘并尝试替换最后一行
#!/usr/python3
import os
import subprocess
sometext = 'Hello World'
subprocess.call('echo {sometext}')
这会出现以下错误;
Shell % python3 pingtest.py
Traceback (most recent call last):
File "pingtest.py", line 12, in <module>
subprocess.call('echo {sometext}')
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 340, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'echo {sometext}'
#!/usr/bin/python3
import subprocess
sometext = 'Hello World'
subprocess.call(["echo", sometext])