import os
1.文件重命名 rename
第一个参数是原文件名,第二个参数是新文件名
os.rename('file/b.txt','bb.txt') 将原本file文件的子模块b.txt移动到了file同级目录,相当于剪切移动
2、删除文件,不能删除文件夹 remove
os.remove('bb.txt') 将移动后的bb.txt文件删除
3、 创建目录
创建一层目录 mkdir
os.mkdir('file01') 在file同级目录创建了一个新的目录file01
4.创建多层目录 makedirs
os.makedirs('file03/file04/file05')
# 递归创造目录,如果目录已存在,报错
os.makedirs('file04/file05',exist_ok=True)
#此种情况目录已经存在,不会报错,将不会再创建
5.删除目录
os.rmdir 删除一层空目录
如果目录不为空目录,将会报错
os.rmdir('file01/file02')
os.rmdir('file03/file04/file05')
6.removedirs 删除多层空目录,如果目录里面有内容则不会删除
os.removedirs('file03/file04/file05')
7.getcwd 获取当前文件所在目录
ret=os.getcwd()
print(ret) ##E:\homework\pythonworksplace\day13
8.listdir 获取目录列表
ret=os.listdir(os.getcwd())
print(ret) 返回列表形式
运行结果:
['.idea', 'b.txt', 'bbb.txt', 'demon01-文件权限操作.py', 'demon02-读操作.py', 'demon03-写操作.py', 'demon04-seek方法.py', 'demon05-with方式.py', 'demon06-乱码.py', 'demon07-阅读电子书翻页.py', 'demon08-os模块.py', 'demon09-学生管理系统.py', 'file', 'file01', 'file03']
9.切换所在目录 chdir()
os.chdir(os.getcwd()+'/file') # lin
os.chdir(os.getcwd()+'\\file')#windows系统下路径分隔符
print(os.getcwd()) #E:\homework\pythonworksplace\day13\file
10.判断文件或文件夹是否存在
ret=os.path.exists('file') file是文件夹返回True
print(ret) #True
ret1=os.path.exists('b.txt') 返回True
print(ret1) #False
11。判断是否为文件
判断所传路径是否为文件,是返回True,不是返回False
ret=os.path.isfile('file/c.txt')
print(ret) True
ret=os.path.isfile('c.txt') c.txt与正在运行的文件不在同一目录,判断时,将只会从同级目录找寻,此时找不到c.txt,返回False
print(ret) False
12.判断是否为目录
ret=os.path.isdir('b.txt')
print(ret) #False b.txt不是目录,返回False
ret1=os.path.isdir('file')
print(ret1) #True
13.获取绝对路径,从磁盘开始找
ret=os.path.abspath('file')
print(ret) #E:\homework\pythonworksplace\day13\file
ret1=os.getcwd() #获取当前文件所在目录
print(ret1) #E:\homework\pythonworksplace\day13
ret=os.path.isabs(ret1)
print(ret1) #E:\homework\pythonworksplace\day13
ret=os.path.isabs(ret1)
print(ret) #True
14.判断是否为绝对路径
.
ret=os.path.isabs('file') #file为当前文件所在的目录,非绝对路径
print(ret) #False
ret1=os.getcwd() #获取当前文件所在目录
print(ret1) #E:\homework\pythonworksplace\day13
ret=os.path.isabs(ret1)
print(ret) #True
15.获取路径中最后部分
ret=os.path.abspath('file03/file04/file05')
print(ret) #E:\homework\pythonworksplace\day13\file03\file04\file05
ret=os.path.basename('file03/file04/file05')
print(ret) #file05
16.获取路径中父目录部分,不管最后是文件还是文件夹。
#获取当前文件所在目录的路径的上一层
print(os.getcwd()) #E:\homework\pythonworksplace\day13
ret=os.path.dirname(os.getcwd())
print(ret) #E:\homework\pythonworksplace
ret1=ret=os.path.abspath('demon08-os模块')
#获取当前文件的绝对路径的上一层
print(ret1) #E:\homework\pythonworksplace\day13\demon08-os模块
ret2=os.path.dirname(ret1)
print(ret2) #E:\homework\pythonworksplace\day13
获取当前文件绝对路径
print(__file__)#E:\homework\pythonworksplace\day13\demon08-os模块
ret=os.path.dirname(__file__)
17.将多个目录组织成路径返回
ret=os.path.join('aa','bb','cc')
print(ret) # aa\bb\cc
18:
(1).getatime()返回path所指向的文件或者目录的最后访问时间
ret=os.path.getatime('file')
print(ret) # 1547101024.0272446 返回时间戳
(2).getctime()查看文件创建时间
ret=os.path.getctime('file')
print(ret) #1547083130.1095338
(3).getmtime()返回path所指向的文件或者目录的最后修改时间
ret=os.path.getmtime('file')
print(ret) #1547101024.0272446
(4).getsize()查看文件的大小
c.txt文件内容为:你好,世界
ret=os.path.getsize('file/c.txt')
print(ret) #13