����λ�ã���ҳ > �����̳� > �̳� > pythonִ��shell����ȡ���
������ϸ������pythonִ��shell����ȡ����ķ��������������ʹ��Shell��̣���������ϸ�Ĵ���ʾ����
��Python��ִ��Shell�����ȡ������ͨ������ʹ��
subprocess
ģ�顣���ģ���������������µĽ��̣����ӵ����ǵ�����/���/����ܵ�������ȡ���ǵķ����롣������һ����ϸ��ʾ����չʾ�����ʹ��
subprocess.run()
������ִ��Shell�����ȡ�������
subprocess.run()
ִ��
ls
�����ȡ���
���ʾ����ִ��
ls
�����Unix/Linux/macOSϵͳ���г���ǰĿ¼�µ��ļ����ļ��У������������������ͷ����롣
import subprocess
# ����Ҫִ�е�����
command = ['ls', '-l'] # ʹ���б���ʽ������ȫ�����Ա���shellע�빥��
# ִ������
# capture_output=True ������ʾ��������������stdout��stderr��
# text=True ������ʾ�������Ϊ�ı�������Python 3.7+����֮ǰ�汾ʹ��universal_newlines=True
result = subprocess.run(command, capture_output=True, text=True)
# ��ȡ����ı�׼���
stdout = result.stdout
# ��ȡ����Ĵ������������еĻ���
stderr = result.stderr
# ��ȡ����ķ�����
returncode = result.returncode
# ��ӡ���
print(f"��׼���:\n{stdout}")
if stderr:
print(f"�������:\n{stderr}")
print(f"������: {returncode}")
# ע�⣺�������ɹ�ִ�У�returncodeͨ��Ϊ0����0ֵ��ʾ�д�����
ע������ ��
��1�� ��ȫ�� ��������ʹ�������б������ַ���������shellע�빥����������Ͳ������б���ʽ�ṩʱ��Python��ֱ�ӽ����Ǵ��ݸ�ϵͳ������ͨ��shell���ͣ��Ӷ������˰�ȫ���ա�
��2��
�ı����ֽ�
��
capture_output=True
��
text=True
����
universal_newlines=True
���ھɰ汾�У������ʹ��������ı����ַ�������ʽ���أ��������ֽڡ�����ڴ����ı����ݺܷ��㣬�����������Ҫ�������������ݣ���ͼ�����Ƶ�ļ������������Ҫ���ֽ���ʽ���������
��3��
������
��ͨ�����
returncode
�����ж������Ƿ�ɹ�ִ�С����
returncode
��Ϊ0���������Ҫ����
stderr
�е���Ϣ��������⡣
��4��
��ƽ̨������
����ʾ���е�
ls -l
������Unix/Linux/macOSϵͳ���еġ���Windowsϵͳ�ϣ����ǿ�����Ҫִ�в�ͬ�������
dir
������������Ҫ��������ĵ��÷�ʽ�����磬ʹ��
shell=True
������ע��������Ӱ�ȫ���գ���
��5�� ���ܿ��� ��Ƶ���������ⲿ���̿��ܻή�ͳ�������ܡ�������ܣ�������Python�ڲ�������⣬���߿���ʹ�ö��߳�/����������д����ⲿ����ĵ��á�
subprocess.run()
������ִ��Shell����
������һ������ϸ�Ĵ���ʾ������չʾ�������Python��ʹ��
subprocess.run()
������ִ��Shell����������������
ls -l
�������������ܳ��ֵĸ�������������ɹ�ִ�С�������ڡ��Լ������׼����ʹ��������
��ע�⣬���ʾ������������һ��Unix/Linux/macOSϵͳ�����У���Ϊ
ls -l
����Щϵͳ��������������Windows�ϣ����ǿ�����Ҫ�滻Ϊ
dir
�����������Ҫ����
shell
������ʹ�ã�����ͨ���������ʹ��
shell=True
�Ա��ⰲȫ���գ���
import subprocess
def run_command(command):
"""
ִ�и������������������ͷ����롣
����:
- command: Ҫִ�е������Ϊ�б����ݣ����� ['ls', '-l']�����Ա���shellע�롣
����:
- output: ����ı�׼���������еĻ�����
- error: ����Ĵ������������еĻ�����
- returncode: ����ķ����롣
"""
try:
# ʹ��subprocess.run()ִ������
# capture_output=True��ʾ����stdout��stderr
# text=True��ʾ�������Ϊ�ı�������Python 3.7+��
result = subprocess.run(command, capture_output=True, text=True, check=True)
# �������ɹ�ִ�У�û���쳣�����򷵻�������ͷ�����
return result.stdout, None, result.returncode
except subprocess.CalledProcessError as e:
# �������ִ��ʧ�ܣ��������0�����򲶻�CalledProcessError�쳣
# �����ش����������׼���������еĻ����ͷ�����
return None, e.stderr, e.returncode
except Exception as e:
# �����������ܵ��쳣����Ȼ������򵥵������п��ܲ�̫������
return None, f"An unexpected error occurred: {e}", None
# ����Ҫִ�е�����
command = ['ls', '-l']
# ִ�������ȡ���
output, error, returncode = run_command(command)
# ���ݷ��صĽ����ӡ��Ӧ����Ϣ
if output:
print("��׼���:")
print(output)
if error:
print("�������:")
print(error)
if returncode is not None:
print(f"������: {returncode}")
if returncode == 0:
print("����ɹ�ִ�С�")
else:
print("����ִ��ʧ�ܡ�")
�����ʾ���У�
run_command
������װ��
subprocess.run()
�ĵ��ã��������˼��ֿ��ܵ������
��1������ɹ�ִ�У�������Ϊ0�������ر�׼�����
None
��Ϊ����������Լ������롣
��2������ִ��ʧ�ܣ��������0��������
subprocess.CalledProcessError
�쳣��������
None
��Ϊ��׼���������������Լ������롣
��3�������쳣��������񲢷���һ��������Ϣ��
None
��Ϊ�����루��Ȼ������ض��������У�����
subprocess.run()
ͨ��ֻ�׳�
CalledProcessError
�������ⲿ�ֿ��ܲ��ᱻִ�У���
��ע�⣬
subprocess.run()
��
check=True
������������ط����˳���ʱ�Զ��׳�
CalledProcessError
�쳣����ʹ�����ǿ�����
try-except
���в�������Ȼ���������ʾ���У���ѡ������ʽ�ز����쳣���Ա��ܹ������ش�������ͷ����롣�������ֻ��������ʧ��ʱ�׳��쳣�����Ҳ����Ĵ������ľ���ϸ�ڣ���ô�����ڵ���
subprocess.run()
ʱ����
check=True
������Python��Ĭ���쳣������������������
Shell�����ָʹ��Shell��Ҳ��Ϊ�����н������������н��棩��Ϊ�����������д�ű��Ĺ��̡�Shell��Unix/Linux/macOS����Unix����ϵͳ�е�һ�����������Ϊ�û��ṩ��һ�������ϵͳ�����Ļ�����Shell�ű���һϵ��Shell����ļ��ϣ���Щ�����д���ı��ļ��У���ͨ��Shell������ִ�У���ʵ���Զ��������������ļ�������ϵͳ��Դ��Ŀ�ġ�
Shell�ű����п�ƽ̨�ԣ���Ϊ������Ҫ������Shell�Ĺ��ܺ��������Щ�ڴ������Unixϵͳ�ж������Ƶġ�Ȼ������ͬ��Shell����Bash��Zsh��Fish�ȣ��������Լ������Ժ���չ����˱�д�Ľű�������Ҫ����ض���Shell�������䡣
Shell���ͨ�������������塢�����жϡ�Ñ�����ơ��������õȱ��Ԫ�أ����봫ͳ���������ȣ�Shell�ű����﷨��Լ�����
Shell�������û���Shell�����������ָ�����ִ�и��ֲ��������ļ�����������ִ�С�ϵͳ�����ȡ�Shell���������Shell���õģ�Ҳ������ϵͳ�ϵ��ⲿ����
��1��
��������
����Shell�����ṩ�������Щ������Shell����ʱ���Ѿ����ص��ڴ��У����ִ���ٶȽϿ졣�������������ϵͳ�ϵ������������������ϵͳ����ʱ���Ѿ����á������������������
cd
���ı�Ŀ¼����
echo
����ʾ��Ϣ����
exit
���˳�Shell���ȡ�
��2��
�ⲿ����
��Ҳ��Ϊ�ļ�ϵͳ�����Щ������ϵͳ�϶����ij���ͨ��λ��
/bin
��
/usr/bin
��
/sbin
��
/usr/sbin
��Ŀ¼�¡���Shell��Ҫִ����Щ����ʱ�����������ЩĿ¼���ҵ���Ӧ�ij���ִ�С��������ⲿ�������
ls
���г�Ŀ¼���ݣ���
cp
�������ļ���Ŀ¼����
mv
���ƶ����������ļ���Ŀ¼���ȡ�
Shell�������ͨ���ܵ���
|
�����ض���
>
��
<
��
>>
���������滻��
command
��
$(command)
���Ȼ��ƽ�����ϣ���ʵ�ָ����ӵIJ��������磬
ls -l | grep '^d'
������г���ǰĿ¼������Ŀ¼����ϸ��Ϣ��
ls -l
�г���ϸ��Ϣ��
grep '^d'
ɸѡ����
d
��ͷ���У���Ŀ¼����
Shell��̺�Shell������Unix/Linux/macOS��ϵͳ�û��ճ������в��ɻ�ȱ�Ĺ��ߣ������ܹ����������û��Ĺ���Ч�ʣ��������û��Զ�������ɸ�������
ʹ��Shell�����Ҫ�漰����дShell�ű�����Щ�ű�������һϵ�е�Shell���ͨ��Shell������ִ����ʵ���ض��Ĺ��ܡ�������ʹ��Shell��̵Ļ������裺
���ȣ�������Ҫȷ��ʹ������Shell��������Shell��Bash��Bourne Again SHell�������Linux���а��Ĭ��Shell����Zsh��Z Shell������������ǿ���Ժ͸��õ��û����飩��Fish��Friendly Interactive SHell�����û��Ѻú�����ѧϰ�����ƣ��ȡ����ڳ�ѧ����˵��Bash��һ���ܺõ���㣬��Ϊ���㷺�������ĵ��ḻ��
Shell�ű�ͨ����������
.sh
Ϊ��չ�����ļ��С����ǿ���ʹ���κ��ı��à¼ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ð´Shell�ű�������
nano
��
vim
��
emacs
��򵥵�
echo
���ض���
������һ���򵥵�Shell�ű�ʾ��������ӡ����Hello, World!����
#!/bin/bash
# ����һ���򵥵�Shell�ű�ʾ��
echo "Hello, World!"
�ڽű��ĵ�һ�У�
#!/bin/bash
����Ϊshebang��������ϵͳ����ű�Ӧ��ʹ���ĸ���������ִ�С�����������У���ָ����Bash��
�����ǵĽű����浽�ļ��У�����
hello.sh
��
��Linux��macOS�ϣ�������Ҫ���ű��ļ�����ִ��Ȩ�ޣ��Ա��ܹ�ֱ�������������ǿ���ʹ��
chmod
������������һ�㣺
bash���ƴ���
chmod +x hello.sh
���������
hello.sh
�ļ�����ִ��Ȩ�ޡ�
���ڣ����ǿ���ͨ���������ַ�ʽ֮һ���������ǵĽű���
ֱ��ͨ���ű���·�������ƣ�����ű�����ִ��Ȩ�ޣ���
bash���ƴ���
./hello.sh
ע�⣬������Ҫʹ��
./
��ָ���ű�λ�ڵ�ǰĿ¼�¡�
ʹ��Shell��������ִ�нű������۽ű��Ƿ����ִ��Ȩ�ޣ���
bash���ƴ���
bash hello.sh
�����������Bash��������ִ��
hello.sh
�ű��е����
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������