PYTHON基础知识学习笔记(十一)

系统工具

概念

系统工具就是用于管理系统的工具。如window的命令行工具,linux下的shell脚本,Mac系统叫终端。Python中提供了模块可以直接操作各系统的系统工具,具体模块有很多,主要讲sys和os,其中os支持跨平台,且os.path提供文件及目录工具。

sys模块

提供一组功能映射Python运行时的操作系统。

>>> import sys
>>> dir(sys)
#查看该模块下的功能 
['__breakpointhook__',
 '__displayhook__',
 '__doc__',
 '__excepthook__',
 '__interactivehook__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '__stderr__',
 '__stdin__',
 '__stdout__',
 '_base_executable',
 '_clear_type_cache',
 '_current_frames',
 '_debugmallocstats',
 '_enablelegacywindowsfsencoding',
 '_framework',
 '_getframe',
 '_git',
 '_home',
 '_xoptions',
 'api_version',
 'argv',
 'base_exec_prefix',
 'base_prefix',
 'breakpointhook',
 'builtin_module_names',
 'byteorder',
 'call_tracing',
 'callstats',
 'copyright',
 'displayhook',
 'dllhandle',
 'dont_write_bytecode',
 'exc_info',
 'excepthook',
 'exec_prefix',
 'executable',
 'exit',
 'flags',
 'float_info',
 'float_repr_style',
 'get_asyncgen_hooks',
 'get_coroutine_origin_tracking_depth',
 'get_coroutine_wrapper',
 'getallocatedblocks',
 'getcheckinterval',
 'getdefaultencoding',
 'getfilesystemencodeerrors',
 'getfilesystemencoding',
 'getprofile',
 'getrecursionlimit',
 'getrefcount',
 'getsizeof',
 'getswitchinterval',
 'gettrace',
 'getwindowsversion',
 'hash_info',
 'hexversion',
 'implementation',
 'int_info',
 'intern',
 'is_finalizing',
 'maxsize',
 'maxunicode',
 'meta_path',
 'modules',
 'path',
 'path_hooks',
 'path_importer_cache',
 'platform',
 'prefix',
 'ps1',
 'ps2',
 'ps3',
 'set_asyncgen_hooks',
 'set_coroutine_origin_tracking_depth',
 'set_coroutine_wrapper',
 'setcheckinterval',
 'setprofile',
 'setrecursionlimit',
 'setswitchinterval',
 'settrace',
 'stderr',
 'stdin',
 'stdout',
 'thread_info',
 'version',
 'version_info',
 'warnoptions',
 'winver']

平台与版本

1、查看当前系统

>>> sys.platform
'win32'

2、查看当前python版本

>>> sys.version
'3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)]'

3、查看python搜索路径

>>> sys.path
['E:\\software\\pycharm\\azwj\\PyCharm 2019.1.3\\helpers\\pydev',
    ······
 'E:/software/pycharm/project']

4、查看python载入的模块

>>> sys.modules

观察异常细节

sys.exc_info()获取最后一次异常细节
可利用traceback.print_tb()函数将具体错误内容显示出。

>>> import sys
>>> import traceback
>>> try:
....    raise KeyError
....except:
....    print(sys.exc_info())
....
#运行结果    
(<class 'KeyError'>, KeyError(), <traceback object at 0x0000014B029D6448>)
----------------------------------------------------------------------------
>>> try:
....   raise KeyError
....except:
....    print(sys.exc_info())
....    traceback.print_tb(sys.exc_info()[2])
....
#运行结果        
(<class 'KeyError'>, KeyError(), <traceback object at 0x0000014B02A3A4C8>)
  File "<input>", line 2, in <module>

命令行参数

sys.argv()
捕获命令行运行时的所有参数,以列表形式返回。

标准流

1、sys.stdin()标准输入流,默认等同于input();
2、sys.stdout()标准输出流,默认等同于print();
3、sys.stderror()标准错误流。

os模块

提供跨平台可移植的操作系统编程接口。

shell变量

os.environ可查看python的一些环境变量

>>> import os
>>> os.environ

管理工具

1、os.getcwd()获取当前工作目录

>>> os.getcwd()
#运行结果
'E:\\software\\pycharm\\project'

2、os.listdir()展示某个目录下的内容,默认为当前工作目录

>>> os.listdir()
#运行结果
['.idea', '.pytest_cache', 'main.py', '__pycache__']

3、os.getpid()获取当前进程的id

>>> os.getpid()
17952

4、os.chdir()改变工作目录
5、os.getppid()获取父进程的id

运行shell命令

1、os.system()在python脚本中运行shell命令

os.system('dir /a') #windows系统
os.system('ls /a')  #linux系统

2、os.popen()运行命令并连接输入输出流

文件处理

1、os.rename(‘旧名’,‘新名’)文件更名
2、os.remove(‘文件名’)删除文件
3、os.mkdir(‘目录名’)创建目录
4、os.rmdir(‘目录名’)删除目录

可移植工具

1、os.sep 当前操作系统的分隔符

>>> os.sep
'\\'

2、os.pathsep 路径分隔符,多个路径的分隔

>>> os.pathsep
';'

3、os.curdir 表示当前目录,为“.”

>>> os.curdir
'.'

4、os.pardir 表示父目录,为“…”

>>> os.pardir
'..'

路径模块 .path

1、os.path.isdir() 判断是否为dir
2、os.path.isfile() 判断是否为文件
3、os.path.exists() 判断是否存在
4、os.path.getsize() 文件有多少字节
5、os.path.split() 将文件名与路径拆分开

>>> name = r'c:\data\temp\data.txt'
>>> os.path.split(name)
Out[78]: ('c:\\data\\temp', 'data.txt')
>>> os.path.dirname(name)   #查看路径
Out[79]: 'c:\\data\\temp'
>>> os.path.basename(name)  #查看文件名
'data.txt'

6、os.path.splitext() 将路径\文件名与扩展名拆分开

>>> os.path.splitext(name)
('c:\\data\\temp\\data', '.txt')

7、os.path.join() 将路径或文件名拼接

>>> os.path.join(r'c:\temp','product.csv')
'c:\\temp\\product.csv'

8、os.path.normpath() 将不规范的路径转化为符合当前系统标准的路径

>>> p = 'D:\\app\\db/files/data.csv'
>>> p
'D:\\app\\db/files/data.csv'
>>> os.path.normpath(p)
'D:\\app\\db\\files\\data.csv'

9、os.path.abspath() 绝对化路径

未完待续!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值