命令行工具、Shell脚本、系统管理
映射python运行时操作系统,完成操作系统的工作
import sys
print(dir(sys))
from pprint import pprint
pprint(dir(sys))
import sys
print(sys.platform)
print(sys.version)
## 简单的列表告诉我们路径
print(sys.path)
win32
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)]
[‘F:\teacher\pythonbasetry\day10’, ‘F:\teacher\pythonbasetry’, ‘C:\Users\97501\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\192.7142.42\helpers\pycharm_display’, ‘D:\python\python37.zip’, ‘D:\python\DLLs’, ‘D:\python\lib’, ‘D:\python’, ‘D:\python\lib\site-packages’, ‘C:\Users\97501\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\192.7142.42\helpers\pycharm_matplotlib_backend’]
最后一个异常具体信息:类型、对象、回溯对象
打印回溯信息列表
import traceback
try:
raise KeyError
except:
## 异常信息:类型,消息,回溯对象
print(sys.exc_info())
traceback.print_tb(sys.exc_info()[2])
(
File “F:/teacher/pythonbasetry/day10/init.py”, line 26, in
raise KeyError
import sys
def add():
a = 5
b = 3
return a + b
print(add())
print(sys.argv)
print(sys.argv[0])
print(sys.argv[1])
print(sys.argv[2])
import sys
def add(a,b):
return a + b
a = 0
b = 0
if sys.argv[1]:
a = int(sys.argv[1])
if sys.argv[2]:
b = int(sys.argv[2])
print(add(a,b))
>>>import sys
>>>sys.stdout.write('Hello')
Hello5
>>>print('输入');sys.stdin.readline()[:]
输入
>? python
>>>print('输入');x = sys.stdin.readline()[:]
输入
>? python
>>>x
'python\n'
>>>sys.stderr.write('mistake')
mistake7
跨平台可移植操作系统接口
>>>import os
>>>os.environ
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\97501\\AppData\\Roaming', 'COMMONPROGRAMFILES': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'DESKTOP-MG006HV', 'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\97501', 'IDEA_INITIAL_DIRECTORY': 'C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\bin', 'IPYTHONENABLE': 'True', 'LOCALAPPDATA': 'C:\\Users\\97501\\AppData\\Local', 'LOGONSERVER': '\\\\DESKTOP-MG006HV', 'NUMBER_OF_PROCESSORS': '12', 'ONEDRIVE': 'C:\\Users\\97501\\OneDrive', 'ONEDRIVECONSUMER': 'C:\\Users\\97501\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;D:\\python;E:\\matlab\\MATLAB anzhuang\\runtime\\win64;E:\\matlab\\MATLAB anzhuang\\bin;%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\;%SYSTEMROOT%\\System32\\OpenSSH\\;C:\\Users\\97501\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\97501\\Anaconda3;C:\\Users\\97501\\Anaconda3\\Scripts;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'x86', 'PROCESSOR_ARCHITEW6432': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 158 Stepping 10, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '9e0a', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files (x86)', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules', 'PUBLIC': 'C:\\Users\\Public', 'PYCHARM_DISPLAY_PORT': '63342', 'PYCHARM_HOSTED': '1', 'PYCHARM_MATPLOTLIB_INDEX': '0', 'PYCHARM_MATPLOTLIB_INTERACTIVE': 'true', 'PYDEVD_LOAD_VALUES_ASYNC': 'True', 'PYTHONIOENCODING': 'UTF-8', 'PYTHONPATH': 'C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pycharm_matplotlib_backend;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pycharm_display;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\third_party\\thriftpy;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pydev', 'PYTHONUNBUFFERED': '1', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\WINDOWS', 'TEMP': 'C:\\Users\\97501\\AppData\\Local\\Temp', 'TMP': 'C:\\Users\\97501\\AppData\\Local\\Temp', 'USERDOMAIN': 'DESKTOP-MG006HV', 'USERDOMAIN_ROAMINGPROFILE': 'DESKTOP-MG006HV', 'USERNAME': '97501', 'USERPROFILE': 'C:\\Users\\97501', 'WINDIR': 'C:\\WINDOWS'})
获取当前工作目录
列举目录内容
改变工作目录
获取当前进程ID
获取当前父进程ID
>>>import os
>>>os.environ
environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\97501\\AppData\\Roaming', 'COMMONPROGRAMFILES': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'DESKTOP-MG006HV', 'COMSPEC': 'C:\\WINDOWS\\system32\\cmd.exe', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\97501', 'IDEA_INITIAL_DIRECTORY': 'C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\bin', 'IPYTHONENABLE': 'True', 'LOCALAPPDATA': 'C:\\Users\\97501\\AppData\\Local', 'LOGONSERVER': '\\\\DESKTOP-MG006HV', 'NUMBER_OF_PROCESSORS': '12', 'ONEDRIVE': 'C:\\Users\\97501\\OneDrive', 'ONEDRIVECONSUMER': 'C:\\Users\\97501\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\iCLS\\;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\DAL;C:\\Program Files (x86)\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\Intel(R) Management Engine Components\\IPT;C:\\Program Files\\Intel\\WiFi\\bin\\;C:\\Program Files\\Common Files\\Intel\\WirelessCommon\\;D:\\python;E:\\matlab\\MATLAB anzhuang\\runtime\\win64;E:\\matlab\\MATLAB anzhuang\\bin;%SystemRoot%\\system32;%SystemRoot%;%SystemRoot%\\System32\\Wbem;%SYSTEMROOT%\\System32\\WindowsPowerShell\\v1.0\\;%SYSTEMROOT%\\System32\\OpenSSH\\;C:\\Users\\97501\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\97501\\Anaconda3;C:\\Users\\97501\\Anaconda3\\Scripts;', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'x86', 'PROCESSOR_ARCHITEW6432': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 158 Stepping 10, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '9e0a', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files (x86)', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\Modules', 'PUBLIC': 'C:\\Users\\Public', 'PYCHARM_DISPLAY_PORT': '63342', 'PYCHARM_HOSTED': '1', 'PYCHARM_MATPLOTLIB_INDEX': '0', 'PYCHARM_MATPLOTLIB_INTERACTIVE': 'true', 'PYDEVD_LOAD_VALUES_ASYNC': 'True', 'PYTHONIOENCODING': 'UTF-8', 'PYTHONPATH': 'C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pycharm_matplotlib_backend;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pycharm_display;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\third_party\\thriftpy;C:\\Users\\97501\\AppData\\Local\\JetBrains\\Toolbox\\apps\\PyCharm-P\\ch-0\\192.7142.42\\helpers\\pydev', 'PYTHONUNBUFFERED': '1', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\WINDOWS', 'TEMP': 'C:\\Users\\97501\\AppData\\Local\\Temp', 'TMP': 'C:\\Users\\97501\\AppData\\Local\\Temp', 'USERDOMAIN': 'DESKTOP-MG006HV', 'USERDOMAIN_ROAMINGPROFILE': 'DESKTOP-MG006HV', 'USERNAME': '97501', 'USERPROFILE': 'C:\\Users\\97501', 'WINDIR': 'C:\\WINDOWS'})
>>>os.getcwd()
'F:\\teacher\\pythonbasetry'
>>>os.listdir()
['.idea', '1_hello.py', '1_result.py', 'day1', 'day10', 'day11', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7', 'day8', 'day9']
>>>os.chdir('day11')
>>>os.getcwd()
'F:\\teacher\\pythonbasetry\\day11'
>>>os.listdir()
['__init__.py']
>>>os.listdir(r'F:')
['__init__.py']
>>>os.getpid()
47348
>>>os.getppid()
41564
python脚本运行shell命令
运行+连接输入输出流
import os
os.popen('dir /b')
<os._wrap_close object at 0x04388A70>
os.popen('1_hello.py')
<os._wrap_close object at 0x012331B0>
text = os.popen('dir /b').read()
text
'.idea\n1_hello.py\n1_result.py\nday1\nday10\nday11\nday2\nday3\nday4\nday5\nday6\nday7\nday8\nday9\n'
Traceback (most recent call last):
File "F:\teacher\pythonbasetry\1_hello.py", line 1, in <module>
print("hello")
OSError: [Errno 22] Invalid argument
results = os.popen('1_hello.py').readlines()
results
['hello\n']
print(results)
['hello\n']
创建目录
删除目录
改名
删除文件
import os
#当前工作目录
print(os.getcwd())
#创建目录
os.mkdir('test')
print(os.listdir())
#进入子目录
os.chdir('test')
print(os.getcwd())
print(os.listdir())
#创建文件
open('info11.txt','w',encoding='utf8').write('hello')
print(os.listdir())
#重命名文件
os.rename('info11.txt','try.txt')
print(os.listdir())
#移除文件
os.remove('try.txt')
#返回上一级
os.chdir('..')
print(os.getcwd())
#删除目录
os.rmdir('test')
D:\python\python.exe F:/teacher/pythonbasetry/day11/init.py
F:\teacher\pythonbasetry\day11
F:\teacher\pythonbasetry\day11\test
[]
[‘info11.txt’]
[‘try.txt’]
F:\teacher\pythonbasetry\day11
分隔符
路径分隔符
相对当前目录符号
相对上级目录符号
import os
#典型分隔符
print(os.sep)
#路径分隔符,一批路径操作时连接多个路径
print(os.pathsep)
#相对目录
#父目录
print(os.curdir)
print(os.pardir)
D:\python\python.exe F:/teacher/pythonbasetry/day11/copy.py
;
.
…
Process finished with exit code 0
提供文件和目录的可移植编程接口
是否目录
是否文件
是否存在
拆分路径
拆分路径扩展名
连接路径
标准化路径
import os
print(os.getcwd())
print(os.listdir())
#判断是否目录
os.path.isdir(r'f:\teacher')
#存在的目录判断是否目录
print(os.path.isdir(r'f:\teacher'))
#不存在的目录判断是否目录
print(os.path.isdir(r'f:teacherteacher'))
#判断是否文件
print(os.path.isfile(r'F:\teacher\pythonbasetry\day11\PATH.py'))
#判断是否存在
print(os.path.exists(r'f:\teacher'))
#大小,单位字节
print(os.path.getsize(r'F:\teacher\pythonbasetry\day11\PATH.py'))
#切割路径
print(os.path.split(r'F:\teacher\pythonbasetry\day11\PATH.py'))
name = r'F:\teacher\pythonbasetry\day11\PATH.py'
print(os.path.dirname(name))
print(os.path.basename(name))
print(os.path.splitext(name))
#连接路径
joint = os.path.join(r'f:\base','try.py')
print(joint)
print(os.sep)
print(os.path.split(name))
print(name.split(os.sep))
#转换成符合当前操作系统标准
p = 'F:\\teacher\\pythonbasetry/day11/PATH.py'
print(p)
Normal = os.path.normpath(p)
print(Normal)
#当前路径转换成绝对路径
print(os.getcwd())
relative = os.path.abspath('..')
print(relative)
D:\python\python.exe F:/teacher/pythonbasetry/day11/PATH.py
F:\teacher\pythonbasetry\day11
[‘copy.py’, ‘PATH.py’, ‘init.py’]
True
False
True
True
1129
(‘F:\teacher\pythonbasetry\day11’, ‘PATH.py’)
F:\teacher\pythonbasetry\day11
PATH.py
(‘F:\teacher\pythonbasetry\day11\PATH’, ‘.py’)
f:\base\try.py
(‘F:\teacher\pythonbasetry\day11’, ‘PATH.py’)
[‘F:’, ‘teacher’, ‘pythonbasetry’, ‘day11’, ‘PATH.py’]
F:\teacher\pythonbasetry/day11/PATH.py
F:\teacher\pythonbasetry\day11\PATH.py
F:\teacher\pythonbasetry\day11
F:\teacher\pythonbasetry