为了更好的通过Python脚本执行linux命令,通过自定义别名(Alias)进行多个命令组合,然而通过python中如何通过subprocess类库
自定义alias
#alias lt='ls --human-readable --size -1 -S --classify'
alias lt='du -sh * | sort -h'
测试调用的Python代码
from subprocess import call
def test():
call("lt")
if __name__ == "__main__":
test()
直接运行上面的代码提示错误信息如下
Traceback (most recent call last):
File "test_alias.py", line 7, in <module>
test()
File "test_alias.py", line 4, in test
call("lt")
File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 323, in call
with Popen(*popenargs, **kwargs) as p:
File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/Users/shiwei/anaconda3/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'lt': 'lt'
通过错误信息可以看出lt命令被当作了文件或目录提示找不到
如果您需要的别名是在 ~/.bashrc 中定义的,可以通过尝试以下几种方式进行运行:
1)你必须给’shell’关键字:
subprocess.call('command', shell=True)
否则,您给定的命令用于查找可执行文件,而不是传递给 shell,它是扩展别名和函数等内容的 shell。
from subprocess import call
def test():
call("lt", shell=True)
if __name__ == "__main__":
test()
运行此处代码发现报错找不到文件或目录的错误了,但是出现了新的错误,错误信息如下
# /bin/sh: lt: command not found
有错误信息可以看出还是找不到lt命令,不过不报错误了(此处应该有掌声)
2) 默认情况下,subprocess.call 使用’/bin/sh’ shell。如果这是您要调用的 Bash 别名,则需要使用“可执行”关键字 告诉子进程使用 bash 而不是 sh:
subprocess.call('command', shell=True, executable='/bin/bash')
代码如下
from subprocess import call
def test():
call("lt", shell=True, executable='/bin/bash')
if __name__ == "__main__":
test()
通过执行代码发现,报错的错误同上,还是提示lt找到
3) 但是,/bin/bash 除非作为“交互式”shell(使用“-i”)启动,否则不会获取 ~/.bashrc。不幸的是,您不能传递 executable=’/bin/bash -i’,因为它认为整个值是可执行文件的名称。因此,如果您的别名是在用户的正常交互式启动中定义的,例如在 .bashrc 中,您必须使用以下替代形式调用命令:
subprocess.call(['/bin/bash', '-i', '-c', 命令])
# i.e. shell=False (the default)
from subprocess import call
def test():
call(['/bin/bash', '-i', '-c', 'lt'])
if __name__ == "__main__":
test()
正常执行,成功调用了alias命令