import os
#os模块帮助文档 version1.0
#os.getcwd() 当前路径
#os.chdir(指定路径) 改变到指定路径
#os.listdir(路径参数) 列出当前路径下所有文件及目录
#os.walk(路径参数[,topdown=true][,onerror=None][,followlinks=Flase]) 遍历目录下所有文件夹及子文件夹,获得路径、文件夹列表、
# 文件列表组成的三元组,默认true从根目录开始遍历;onerror指定一个callable对象,walk异常时调用;followlinks=true时遍历快捷方式,linux下指
#软连接symbolic link
#os.path.exists(路径参数) 判断指定目录是否存在
#os.mkdir(路径) 创建单层文件夹,如果已经存在会报错
#os.makedirs(路径) 可创建多层文件夹,注意是否已经存在
#os.rmdir(路径) 删除空文件夹
#os.removedirs(路径) 递归删除目录,不为空则报错
#os.rename(oldname,newname) 重命名目录或文件名
#os.renames(oldname,newname) 递归重命名目录或文件名
#os.path.join(路径1,路径2) 路径合并
#os.path.split(路径) 拆分路径,分为绝对路径和文件名,返回元组
#os.path.dirname(路径) 只获得绝对路径
#os.path.basename(路径) 只获得文件名
#os.path.isdir(参数) 判断是否是文件夹
#os.path.isfile(参数) 判断是否是文件
#os.path.sep 获得路径分隔符
#os.path.getsize(文件参数) 获得文件大小,默认字节
#os.open(file,flags[,mode]) flags参数:os.O_RDONLY只读,os.WRONLY只写,os.O_RDWR读写,os.O_NONBLOCK打开时不阻塞,os.O_APPEND追加
#方式打开,os.O_CREAT 创建并打开一个文件,os.O_TRUNC打开文件并将长度截断为零,os.O_EXCL如果指定文件存在则返回错误,os.O_SHLOCK自动获取共享锁
#os.O_EXLOCK自动获取独立锁,os.O_DIRECT 消除或减少缓存效果,os.O_FSYNC同步写入,os.O_NOFOLLOW不追踪软连接
#os.write(文件名,写入内容)
#os.fsync(文件)强制将文件名写入硬盘
#os.lseek(fd,pos,how)pos相对于给定how在文件中的位置,how参数SEEK_SET或0从文件头开始,SEEK_CUR或1从当前位置开始,SEEK_END或2从文件尾开始
#os.read(fd,n)从文件最多读取n个字节
#os.close(fd) 关闭文件
#
curPath = os.getcwd()
print(curPath)
print(os.listdir(curPath))
print("os.walk 开始")
print(os.walk(curPath))
for pathi,diri,filei in os.walk(curPath):
print(pathi)
print(diri)
print(filei)
print("os.walk 结束")
if os.path.exists(curPath):
print("curPath 存在")
else:
print("curPath 不存在")
os.rmdir(curPath + "\\testFile20220926")
os.mkdir(curPath + "\\testFile20220926")
os.removedirs(curPath + "\\testFile20220926c\\erceng")
#报错os.mkdir(curPath + "\\testFile20220926b\\erceng")
os.makedirs(curPath + "\\testFile20220926c\\erceng")
#os.mkdir(curPath + "\\testFile20220926\\file001.txt")
#os.rename(curPath + "\\testFile20220926",curPath + "\\testFile20220926rename")
list1 = ["a1.txt","a2.jpg","a3.xls"]
for i in list1:
pathjoin = os.path.join(curPath,i)
print(pathjoin)
pathSplit = os.path.split(curPath + "\\p2_1.py")
print(pathSplit)
curPathFile = curPath + "\\pythonOsTest.py"
print(os.path.dirname(curPathFile))
print(os.path.basename(curPathFile))
for i in os.listdir():
if os.path.isdir(i):
print("是文件夹:" + i)
for i in os.listdir():
if os.path.isfile(i):
print("是文件:" + i)
print(os.sep)
print(os.path.getsize(curPath + "\\data_new.xlsx"))
filePath = curPath + "\\fileOpenTest.txt"
file = os.open(filePath,os.O_CREAT | os.O_RDWR)
line = str.encode("this is fileWriteTest!!")
os.write(file,line)
os.fsync(file)
os.lseek(file,0,0)
text = os.read(file,200)
print(text)
os.close(file)