import os
def main():
# try:
# # os.mkdir("PythonLearn",0777)
# os.mkdir("PythonLearn")
# except Exception as ret:
# print("目录已经存在:",ret)
# try:
# os.rmdir("PythonLearn")
# print("delete sucess")
# except Exception as ret:
# print(type(ret))
# print("delete fail:",ret)
# 这个可以作用于目录与文件
# try:
# os.rename("pinyin.txt","snd.txt")
# except Exception as re:
# """" FileExistsError: 如果sdn.txt存在
# FileNotFoundError:如果pinyin.txt不存在
# """
# print(type(re))
# print(re)
# try:
# # 只这个只能删除文件,不能删除目录
# os.remove("build")
# except Exception as re:
# '''
# 文件不存在:[WinError 2] 系统找不到指定的文件。: 'snd.txt'
# 目录:[WinError 5] 拒绝访问。: 'build'
# '''
# print(re)
# print(os.getcwd())
# """
# 输出的格式:L:\DEV_git\XYD\电子词典资料\多本sqlite3字典\sqlite3
# """
"""df
os.chdir 返回值为None
"""
# print(os.chdir(r"L:\DEV_git\XYD\电子词典资料\多本sqlite3字典\sqlite3\build"))
# for i in os.listdir():
# print(i)
# for root, dirs, filenames in os.walk(r"L:\DEV_git\RUIZU\XYD5705_C\case\drv\I2C",topdown = True):
# print("root:",root)
# for i in dirs:
# print("dir:",i)
#
# for i in filenames:
# print("file:",i)
# print(os.path.basename('/root/runoob.txt')) # 返回文件名
# print(os.path.dirname('/root/runoob.txt')) # 返回目录路径
# print(os.path.split('/root/runoob.txt')) # 分割文件名与路径
# print(os.path.join('root', 'test', 'runoob.txt')) # 将目录和文件名合成一个路径 输出root\test\runoob.txt
# 如果文件不存在,或者为目录返回False ,否则返回True
# print(os.path.isfile("L:\DEV_git\RUIZU\XYD5705_C\case\drv\I2C\I2C.c"))
# 如果目录不存在,或者为文件返回False ,否则返回True
# print(os.path.isdir("L:\DEV_git\RUIZU\XYD5705_C\case\drv\I2C"))
# print(os.path.isdir("L:/kdk"))
'''
('L:\\DEV_git\\RUIZU\\XYD5705_C\\case\\drv\\I2C\\I2C', '.c')
'''
# print(os.path.splitext("L:\DEV_git\RUIZU\XYD5705_C\case\drv\I2C\I2C.c"))
# 在str前面加上当前的完整路径名
# print(os.path.abspath("str"))
# 返回文件的大小,以字节为单位, 如果是目录,则返回0
try:
print(os.path.getsize("L:\DEV_git\RUIZU\XYD5705_C\case\drv\I2C\I2C.c"))
except Exception as re:
print(re)
if __name__ == "__main__":
main()
Python os模块 常见函数测试
最新推荐文章于 2023-06-15 10:21:11 发布