京公网安备 11010802034615号
经营许可证编号:京B2-20210330
作者:小伍哥
来源:AI入门学习
os,语义操作系统,所以该模块就是操作系统相关的功能了,用于处理文件和目录这些我们日常手动需要做的操作,比如新建文件夹、获取文件列表、删除某个文件、获取文件大小、重命名文件、获取文件修改时间等,该模块就包含了大量的操作系统操作函数,精选常用的进行解析,希望对大家有所帮助。
os下面有很多函数,调用方式一般为os.name,其中比较特殊的是Path,Path模块是OS的一个子模块,下面又有很多的函数,调用方式一般为os.path.isfile。刚接触的可能不大明白,需要注意下。
#加载
import os
#查看os下的函数
print(dir(os))
#查看os.path下的函数
print(dir(os.path))
01、os.name()
描述:显示当前使用的平台,'nt'表示Windows,'posix' 表示Linux
语法:os.name
os.name
'nt'
02、os.getcwd()
描述:返回当前进程的工作目录。
语法:os.getcwd()
os.getcwd()
'C:\\Users\\wuzhengxiang'
03、os.chdir()
描述:改变当前工作目录到指定的路径。
语法:os.chdir(path)
#查看当前目录os.getcwd()
'C:\\Users\\wuzhengxiang'#重新设置当前工作空间
os.chdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
#再次查看当前目录,已经变成新的了
os.getcwd()
'C:\\Users\\wuzhengxiang\\Desktop\\股票数据分析'
04、os.makedirs()
描述:方法用于递归创建目录。像 mkdir(), 但创建的所有intermediate-level文件夹需要包含子目录。
语法:os.makedirs(path, mode=0o777)
os.makedirs('C:/Users/wuzhengxiang/Desktop/股票数据分析/1122', mode=0o777)
05、os.mkdir()
描述:以数字权限模式创建目录。默认的模式为 0777 (八进制)。
语法:os.mkdir(path[, mode])
#创建新的目2233
os.mkdir('C:/Users/wuzhengxiang/Desktop/股票数据分析/2233', mode=0777 )
06、os.listdir()
描述:列出目录下的所有文件和文件夹
语法:os.listdir(path)
os.listdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
os.listdir('.')
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html']
07、os.remove()
描述:用于删除指定路径的文件。如果指定的路径是一个目录,将抛出OSError。
语法:os.remove(path)
os.remove('C:/Users/zhengxiang.wzx/Desktop/timg.jpg')
08、os.rename()
描述:命名文件或目录,能对相应的文件进行重命名
语法:os.rename(src, dst)
参数
#空间设置
data_path = 'C:/Users/zhengxiang.wzx/Desktop/微博情绪识别'
os.chdir(data_path)#设置工作空间
os.getcwd()
'C:\\Users\\zhengxiang.wzx\\Desktop\\微博情绪识别'
os.rename("图片下载.py","图片下载1.py")
09、os.renames()
描述:用于递归重命名目录或文件。类似rename()。既可以重命名文件, 也可以重命名文件的上级目录名
语法:os.renames(old, new)
参数:
os.chdir('C:/Users/wuzhengxiang/Desktop/Python知识点总结')
os.getcwd()
#文件夹和文件同时命名
os.renames("test/Python 63个内置函数详解.py","test2/内置函数详解.py")
os.listdir()
['kaggle',
'test2',
'股票分析',
'课程资源'
10、os.linesep()
描述:当前平台用于分隔(或终止)行的字符串。它可以是单个字符,如 POSIX 上是 '\n',也可以是多个字符,如 Windows 上是 '\r\n'。在写入以文本模式(默认模式)打开的文件时,请不要使用 os.linesep 作为行终止符,请在所有平台上都使用一个 '\n' 代替。
语法:os.linesep
os.linesep
'\r\n'
11、os.pathsep()
描述:操作系统通常用于分隔搜索路径(如 PATH)中不同部分的字符,如 POSIX 上是 ':',Windows 上是 ';'。在 os.path 中也可用。
语法:os.pathsep
os.pathsep
';'
12、os.close()
描述:关闭指定的文件描述符 fd
语法:os.close(fd)
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
os.write(fd, bytes("This is test", encoding = "utf8"))
os.close( fd )
13、os.stat()
描述:获取文件或者目录信息
语法:os.stat(path)
os.stat('C:/Users/wuzhengxiang/Desktop/股票数据分析\\pi.txt')
os.stat_result(st_mode=33206, st_ino=22236523160361562, st_dev=2419217970, st_nlink=1
, st_uid=0, st_gid=0, st_size=53, st_atime=1589638199, st_mtime=1589638199, st_ctime=1581868007)
14、os.sep()
描述:显示当前平台下路径分隔符,在 POSIX 上是 '/',在 Windows 上是是 '\\'
语法:os.sep
os.sep'
\\'
15、os.path.abspath()
描述:返回文件的绝对路径
语法:os.path.abspath(path)
#Excel文件
os.path.abspath('all_data.xlsx')
'C:\\Users\\zhengxiang.wzx\\all_data.xlsx'
#图片文件
os.path.abspath('IMG_7358.JPG')
'C:\\Users\\zhengxiang.wzx\\IMG_7358.JPG'
16、os.path.basename()
描述:返回文件名,纯粹字符串处理逻辑,路径错误也可以
语法:os.path.basename(path)
os.path.basename('C:\\Users\\zhengxiang.wzx\\all_data.xlsx')
'all_data.xlsx'
17、os.path.commonprefix()
描述:返回list(多个路径)中,所有path共有的最长的路径
语法:os.path.commonprefix(list)
os.path.commonprefix(['http://c.biancheng.net/python/aaa', 'http://c.biancheng.net/shell/'])'http://c.biancheng.net/'os.path.commonprefix(['http://bianc/python/aaa', 'http://c.biancheng.net/shell/'])'http://'
18、os.path.dirname()
描述:返回文件路径
语法:os.path.dirname(path)
os.path.dirname('C://my_file.txt')
'C://'
os.path.dirname('C://python//my_file.txt')
'C://python'
19、os.path.exists()
描述:如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。
语法:os.path.exists(path)
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票数据分析/')
True
os.path.exists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi_01.txt')
Fals
20、os.path.lexists()
描述:路径存在则返回True,路径损坏也返回True, 不存在,返回 False。
语法:os.path.lexists
os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
True
os.path.lexists('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi_01.txt')
False
21、os.path.expanduser()
描述:把path中包含的"~"和"~user"转换成用户目录
语法:os.path.expanduser(path)
os.path.expanduser('~/wuzhengxiang/Desktop/股票数据分析/')
'C:\\Users\\wuzhengxiang/wuzhengxiang/Desktop/股票数据分析/'
22、os.path.expandvars()
描述:根据环境变量的值替换path中包含的"$name"和"${name}"
语法:os.path.expandvars(path)
os.environ['KITTIPATH'] = 'D:/thunder'
path = '$KITTIPATH/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'
os.path.expandvars(path)
'D:/thunder/train/2011_09_26_drive_0001_sync/proj_depth/velodyne_raw/image_02/0000000013.png'
23、os.path.getatime()
描述:返回最近访问时间(浮点型秒数),从新纪元到访问时的秒数。
语法:os.path.getatime(path)
os.path.getatime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
1589638199.1343248
24、os.path.getmtime()
描述:返回最近文件修改时间,从新纪元到访问时的秒数。
语法:os.path.getmtime(path)
os.path.getmtime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
1583069050.8148942
25、os.path.getctime()
描述:返回文件 path 创建时间,从新纪元到访问时的秒数。
语法:os.path.getctime(path)
os.path.getctime('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
1581868007.6123319
26、os.path.getsize()
描述:返回文件大小,如果文件不存在就返回错误
语法:os.path.getsize(path)
os.path.getsize('C:/Users/wuzhengxiang/Desktop/股票数据分析/test.gif')
1128677
27、os.path.isabs()
描述:判断是否为绝对路径,也就是说在WIndow系统下,如果输入的字符串以" / "开头,os.path.isabs()就会返回True
语法:os.path.isabs(path)
os.path.isabs('D:/thunder')
True
os.path.isabs('D:\thunder')
False
os.path.isabs('D:\\thunder')
True
28、os.path.isfile()
描述:判断路径是否为文件
语法:os.path.isfile(path)
#文件不存在 返回False
os.path.isfile("C:/Users/wuzhengxiang/Desktop/股票数据分析/pi_01.txt")
False
os.path.isfile("C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt")
True#不是文件 返回Falseo
s.path.isfile("C:/Users/wuzhengxiang/Desktop/股票数据分析/")
False
29、os.path.isdir()
描述:判断路径是否为目录
语法:os.path.isdir(path)
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票数据分析')
True
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票数据分析1')
False
os.path.isdir('C:/Users/wuzhengxiang/Desktop/股票数据分析/pi.txt')
False
30、os.path.join()
描述:把目录和文件名合成一个路径,1.如果各组件名首字母不包含’/’,则函数会自动加上,2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃,3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾
语法:os.path.join(path1[, path2[, ...]])
os.path.join('C:/Users','wuzhengxiang/Desktop/','股票数据分析')
'C:/Users\\wuzhengxiang/Desktop/股票数据分析'
Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'
Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
Path10 = homedevelopcode
Path20 = home\develop\code
31、os.path.normcase()
描述:转换path的大小写和斜杠
语法:os.path.normcase(path)
os.path.normcase('D:\Python\test\data.txt')
'd:\\python\test\\data.txt'
os.path.normcase('c:/WINDOWS\\system64\\')
'c:\\windows\\system64\\'
32、os.path.normpath()
描述:规范path字符串形式
语法:os.path.normpath(path)
os.path.normpath('c://windows\\System32\\../Temp/')
'c:\\windows\\Temp'
33、os.path.realpath()
描述:返回path的真实路径
语法:os.path.realpath(path)
os.path.relpath('C:\\Users\\Administrat\\代码TRY\\test.ipynb', '代码TRY')
'..\\..\\..\\..\\Administrat\\代码TRY\\test.ipynb'
34、os.path.relpath()
描述:返回从当前目录或 start 目录(可选)到达 path 之间要经过的相对路径。这仅仅是对路径的计算,不会访问文件系统来确认 path 或 start 的存在性或属性。
语法:os.path.relpath(path[, start])
os.path.relpath('C:/Users/wuzhengxiang/Desktop/股票数据分析\\test.gif')
'test.gif'
35、os.path.samefile( )
描述:判断目录或文件是否相同
语法:os.path.samefile(path1, path2)
os.path.samefile('C:\\Users', 'C:\\Users')True
os.path.samefile('C:\\Users', 'C:/Users')True
os.path.samefile('C:\\Users', 'C:/Users/wuzhengxiang')False
36、os.path.split()
描述:把路径分割成 dirname 和 basename,返回一个元组
语法:os.path.split(path)
os.path.split('D:\Python\test\data.txt')
('D:\\Python\test', 'data.txt')
37、os.path.splitdrive()
37、os.path.splitdrive()
描述:一般用在 windows 下,返回驱动器名和路径组成的元组
语法:os.path.splitdrive(path)
os.path.splitdrive('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
('C:', '/Users/zhengxiang.wzx/IMG_7358.JPG')
38、os.path.splitext()
描述:分割路径,返回路径名和文件扩展名的元组
语法:os.path.splitext(path)
os.path.splitext('C:/Users/zhengxiang.wzx/IMG_7358.JPG')
('C:/Users/zhengxiang.wzx/IMG_7358', '.JPG')
39、os.path.walk()
描述:遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数
语法:os.path.walk(path, visit, arg)
list(os.walk(abs_cur_dir))
[('C:/Users/wuzhengxiang/Desktop/股票数据分析',
['1122'],
['ETF研究.py', 'foo.txt', 'pi.txt', 'render.html', 'test.gif']),
('C:/Users/wuzhengxiang/Desktop/股票数据分析\\1122', [], [])]
#穷举遍历一个文件夹里面的所有文件,并获取文件的目录名
abs_cur_dir ='C:/Users/wuzhengxiang/Desktop/股票数据分析'
file_url=[]
for dirs,folders,files in os.walk(abs_cur_dir):
for i in files:
file_url.append(os.path.join(dirs,i))
file_url
['C:/Users/wuzhengxiang/Desktop/股票数据分析\\ETF研究.py',
'C:/Users/wuzhengxiang/Desktop/股票数据分析\\foo.txt',
'C:/Users/wuzhengxiang/Desktop/股票数据分析\\pi.txt',
'C:/Users/wuzhengxiang/Desktop/股票数据分析\\render.html',
'C:/Users/wuzhengxiang/Desktop/股票数据分析\\test.gif']
#pathlib也能实现类似的
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
在Python面向对象编程(OOP)中,类方法是构建模块化、可复用代码的核心载体,也是实现封装、继承、多态特性的关键工具。无论是 ...
2026-02-27在MySQL数据库优化中,索引是提升查询效率的核心手段—— 面对千万级、亿级数据量,合理创建索引能将查询时间从秒级压缩到毫秒级 ...
2026-02-27在数字化时代,企业积累的海量数据如同散落的珍珠,若缺乏有效的梳理与分类,终将难以发挥实际价值。CDA(Certified Data Analys ...
2026-02-27在问卷调研中,我们常遇到这样的场景:针对同一批调查对象,在不同时间点(如干预前、干预后、随访期)发放相同或相似的问卷,收 ...
2026-02-26在销售管理的实操场景中,“销售机会”是核心抓手—— 从潜在客户接触到最终成交,每一个环节都藏着业绩增长的关键,也暗藏着客 ...
2026-02-26在CDA数据分析师的日常工作中,数据提取、整理、加工是所有分析工作的起点,而“创建表”与“创建视图”,则是数据库操作中最基 ...
2026-02-26在机器学习分析、数据决策的全流程中,“数据质量决定分析价值”早已成为行业共识—— 正如我们此前在运用机器学习进行分析时强 ...
2026-02-25在数字化时代,数据已成为企业决策、行业升级的核心资产,但海量杂乱的原始数据本身不具备价值—— 只有通过科学的分析方法,挖 ...
2026-02-25在数字化时代,数据已成为企业核心资产,而“数据存储有序化、数据分析专业化、数据价值可落地”,则是企业实现数据驱动的三大核 ...
2026-02-25在数据分析、机器学习的实操场景中,聚类分析与主成分分析(PCA)是两种高频使用的统计与数据处理方法。二者常被用于数据预处理 ...
2026-02-24在聚类分析的实操场景中,K-Means算法因其简单高效、易落地的特点,成为处理无监督分类问题的首选工具——无论是用户画像分层、 ...
2026-02-24数字化浪潮下,数据已成为企业核心竞争力,“用数据说话、用数据决策”成为企业发展的核心逻辑。CDA(Certified Data Analyst) ...
2026-02-24CDA一级知识点汇总手册 第五章 业务数据的特征、处理与透视分析考点52:业务数据分析基础考点53:输入和资源需求考点54:业务数 ...
2026-02-23CDA一级知识点汇总手册 第四章 战略与业务数据分析考点43:战略数据分析基础考点44:表格结构数据的使用考点45:输入数据和资源 ...
2026-02-22CDA一级知识点汇总手册 第三章 商业数据分析框架考点27:商业数据分析体系的核心逻辑——BSC五视角框架考点28:战略视角考点29: ...
2026-02-20CDA一级知识点汇总手册 第二章 数据分析方法考点7:基础范式的核心逻辑(本体论与流程化)考点8:分类分析(本体论核心应用)考 ...
2026-02-18第一章:数据分析思维考点1:UVCA时代的特点考点2:数据分析背后的逻辑思维方法论考点3:流程化企业的数据分析需求考点4:企业数 ...
2026-02-16在数据分析、业务决策、科学研究等领域,统计模型是连接原始数据与业务价值的核心工具——它通过对数据的规律提炼、变量关联分析 ...
2026-02-14在SQL查询实操中,SELECT * 与 SELECT 字段1, 字段2,...(指定个别字段)是最常用的两种查询方式。很多开发者在日常开发中,为了 ...
2026-02-14对CDA(Certified Data Analyst)数据分析师而言,数据分析的核心不是孤立解读单个指标数值,而是构建一套科学、完整、贴合业务 ...
2026-02-14