completeProcess = subprocess.run("la")
这是我的通过python运行命令的代码,它会引发此错误:
Traceback (most recent call last):
File "script_senior.py", line 171, in <module>
completeProcess = subprocess.run("la") # doesn't capture output
File "/usr/lib/python3.5/subprocess.py", line 693, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'la'
我的操作系统是ubuntu,在终端中输入“ la”时,它将在当前工作目录中列出文件(包括隐藏文件)。和“ ll” cmd将列出文件详细信息。但是我不能在python的子进程api中使用它们
如何实现此功能?是否有任何通用的api可以运行命令,或者只是python无法识别“ la”
答案 0 :(得分:2)
la是'ls -A'的别名。在Ubuntu之类的系统中,这是默认的Shell别名。如果您想运行它,只需使用:
completeProcess = subprocess.run(["ls","-A"])
如果您正在寻找标准输出,则可以使用类似以下内容的
:completeProcess = subprocess.run(["ls","-A"] , stdout=subprocess.PIPE)
print(completeProcess.stdout.decode())