python(四)os模块、sys模块

一、os模块

os 模块提供了很多程序与操作系统直接交互的功能

名称 描述 示例
os.getcwd() 得到当前工作目录,即当前Python脚本工作的目录路径 'D:\python'
os.listdir() 返回指定目录下的所有文件和目录名 >>> os.listdir()
os.remove() 函数用来删除一个文件 >>> os.remove('i.cfg')
os.removedirs(r"c:\python") 删除多个目录 os.removedirs('haha')
os.path.isfile() 检验给出的路径是否是一个文件,文件存在为True os.path.isfile("123.txt")
os.path.isdir() 检验给出的路径是否是一个目录,目录存在为True os.path.isdir("work")
os.path.isabs() 判断是否是绝对路径,路径并不需要存在 os.path.isabs("d://242")
os.path.exists() 检验给出的路径是否真地存 os.path.exists("123.txt")
os.path.split() 返回一个路径的目录名和文件名,路径不一定存在 >>> os.path.split('d://hah/123.txt')('d://hah', '123.txt')
os.path.splitext() 分离扩展名,路径不一定存在 >>> os.path.splitext('d://hah/123.txt')('d://hah/123', '.txt')
os.path.dirname() 获取路径名,路径不一定存在 >>> os.path.dirname('d://hah/123.txt')'d://hah'
os.path.abspath() 获得绝对路径,文件或目录不一定存在 os.path.abspath('hehe.txt')'f:\prictice\hehe.txt'
os.path.basename() 获取文件名 os.path.basename("f:\prictice\hehe.txt")'hehe.txt'
os.path.getsize(filename) 获取文件大小 os.path.getsize("321.txt")
os.path.join(dir,filename) 结合目录名与文件名 >>> os.path.join('haha','123.txt')'haha\123.txt'
os.system() 运行shell命令 >>> os.system("python -version")
os.getenv("HOME") 读取操作系统环境变量HOME的值 >>> os.getenv("HOME")'C:\Users\Administrator'
os.environ 返回操作系统所有的环境变量
os.environ.setdefault('HOME','/home/alex') 设置系统环境变量,仅程序运行时有效 os.environ.setdefault('HOME','123')
os.linesep 给出当前平台使用的行终止符 >>> os.linesep '\r\n'
os.name 指示你正在使用的平台 >>> os.name 'nt'
os.curdir 指代当前目录('.')
os.sep 输出操作系统特地党的路径分隔符windows下为'\',linux下为'/'
os.pardir 指代上一级目录('...')
os.rename(old, new) 重命名,也相当于剪切 os.rename('123.txt','321.txt')
os.makedirs(r"c:\python\test") 创建多级目录 os.makedirs("a/b/c")
os.mkdir("test") 创建单个目录 os.mkdir('haha')
os.stat(file) 获取文件或者目录属性 os.stat('321.txt')
os.chmod(path, mode) 修改文件权限与时间戳 os.chmod("/tmp/foo.txt", stat.S_IXGRP)
os.chdir(dirname) 改变工作目录到 os.chdir('d://python')
os.get_terminal_size() 获取当前终端的大小
os.kill(10884,signal.SIGKILL) 杀死进程
getatime(file) 获取文件最近的访问时间,返回浮点秒数
getctime(file) 获取文件的创建时间,返回浮点秒数
getmtime(file) 获取文件最近的修改时间,返回浮点秒数
walk(top) 遍历top参数指定路径下的所有子目录,返回一个三元组(路径,[目录],[文件])生成器 for i in os.walk("F:\prictice"):print(i('F:\prictice', ['work'], ['321.txt', 'conf.ini', 'module.py'])('F:\prictice\work', [], ['543.txt', '榕基.txt'])

二、sys模块

sys 模块是与 Python 解释器交互的一个接口。sys 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分

名称 描述
sys.argv 命令行参数List,第一个元素是程序本身路径
sys.exit(n) 退出程序,正常退出时exit(0)
sys.version 获取Python解释程序的版本信息
sys.maxsize 最大的Int值
sys.path 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform 返回操作系统平台名称
sys.stdout.write('please:') 标准输出 , 引出进度条的例子, 注,在py3上不行,可以用print代替
val = sys.stdin.readline()[:-1] 标准输入
sys.getrecursionlimit() 获取最大递归层数
sys.setrecursionlimit(1200) 设置最大递归层数
sys.getdefaultencoding() 获取解释器默认编码
sys.getfilesystemencoding 获取内存数据存到文件里的默认编码
相关推荐
dulu~dulu38 分钟前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
编程之升级打怪1 小时前
用Python语言实现简单的Redis缓冲数据库驱动库
redis·python
电商API&Tina2 小时前
电商数据采集API接口||合规优先、稳定高效、数据精准
java·javascript·数据库·python·json
玲娜贝儿--努力学习买大鸡腿版2 小时前
hot 100 刷题记录(1)
数据结构·python·算法
兮℡檬,2 小时前
答题卡识别判卷
开发语言·python·计算机视觉
阆遤2 小时前
利用TRAE对nanobot进行安全分析并优化
python·安全·ai·trae·nanobot
雕刻刀3 小时前
ERROR: Failed to build ‘natten‘ when getting requirements to build wheel
开发语言·python
何双新3 小时前
Odoo 技术演进全解析:从 Widget 到 Owl,从 Old API 到声明式 ORM
python
山川行3 小时前
关于《项目C语言》专栏的总结
c语言·开发语言·数据结构·vscode·python·算法·visual studio code
星辰徐哥3 小时前
C语言游戏开发:Pygame、SDL、OpenGL深度解析
c语言·python·pygame