os模块
【一】文件操作
(1)导入模块
(2)获取当前文件路径(abspath)
| file_path = os.path.abspath(__file__) |
(3)获取当前文件所在文件夹的路径(dirname)
| base_dir = os.path.dirname(__file__) |
(4)判断当前路径是否存在(exists)
| |
| path_one = 'D:\2023propygo\ATM.py' |
| base_dir = os.path.exists(path_one) |
| print(base_dir) |
| |
| |
| path_one = 'D:\2023propygo' |
| base_dir = os.path.exists(path_one) |
| print(base_dir) |
(5)拼接路径(join)
| path=os.path.join(r'D:\2023propygo\day\day15\day15','无参装饰器练习.py') |
| |
| base_dir = os.path.dirname(__file__) |
| path= os.path.join(base_dir,'img') |
(6)切割路径(split)
| import os |
| |
| |
| BASE_DIR = os.path.abspath(__file__) |
| |
| file_path_list = os.path.split(BASE_DIR) |
| print(file_path_list) |
(7)获取结尾文件/文件夹名(basename)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| file_path = os.path.join(BASE_DIR, 'img') |
| print(file_path) |
| |
| |
| |
| |
| a = os.path.basename(file_path) |
| print(a) |
| |
| |
| file_path_one = os.path.abspath(__file__) |
| print(file_path_one) |
| |
| print(os.path.basename(file_path_one)) |
| |
(8)判断当前路径是否是文件(isfile)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| print(BASE_DIR) |
| |
| file_path = os.path.abspath(__file__) |
| print(file_path) |
| |
| is_true_one = os.path.isfile(BASE_DIR) |
| print(is_true_one) |
| |
| is_true_two = os.path.isfile(file_path) |
| print(is_true_two) |
(9)判断当前路径是否为绝对路径(isabs)
| import os |
| |
| file_path_one = r'D:\2023propygo\ATM.py' |
| file_path_two = r'test.py' |
| |
| is_true_one = os.path.isabs(file_path_one) |
| print(is_true_one) |
| |
| is_true_two = os.path.isabs(file_path_two) |
| print(is_true_two) |
(10)判断当前文件目录是否存在(isdir)
| import os |
| |
| file_path_one = r'D:\2023propygo\day\test.py' |
| file_path_two = r'test.py' |
| |
| is_true_one = os.path.isdir(file_path_one) |
| print(is_true_one) |
| |
| |
| is_true_two = os.path.isdir(file_path_two) |
| print(is_true_two) |
(11)获取当前文件或目录的最后访问时间(getatime)
| import os, time |
| |
| BASE_DIR = os.path.dirname(__file__) |
| file_path = os.path.abspath(__file__) |
| |
| file_base_time = os.path.getatime(BASE_DIR) |
| print(file_base_time) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_base_time))) |
| |
| |
| file_time = os.path.getatime(file_path) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_time))) |
| |
(12)获取当前文件或者目录的创建时间(getctime)
| import os, time |
| |
| BASE_DIR = os.path.dirname(__file__) |
| file_path = os.path.abspath(__file__) |
| |
| file_base_time = os.path.getctime(BASE_DIR) |
| print(file_base_time) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_base_time))) |
| |
| |
| file_time = os.path.getctime(file_path) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_time))) |
| |
(13)返回当前文件或路径的最后修改时间(getmtime)
| import os, time |
| |
| BASE_DIR = os.path.dirname(__file__) |
| file_path = os.path.abspath(__file__) |
| |
| file_base_time = os.path.getmtime(BASE_DIR) |
| print(file_base_time) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_base_time))) |
| |
| |
| file_time = os.path.getmtime(file_path) |
| print(time.strftime("%Y-%m-%d %H-%M-%S", time.localtime(file_time))) |
| |
(14)返回当前文件的大小(getsize)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| file_path = os.path.abspath(__file__) |
| |
| file_base_time = os.path.getsize(BASE_DIR) |
| print(file_base_time) |
| |
| file_time = os.path.getsize(file_path) |
| print(file_time) |
【二】路径操作
(1)创建单级文件夹(mkdir)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| print(BASE_DIR) |
| |
| file_name = 'img' |
| file_path = os.path.join(BASE_DIR, file_name) |
| print(file_path) |
| |
| |
| if not os.path.exists(file_path): |
| |
| os.mkdir(file_path) |
| print(f"{file_path} :>>> 已创建") |
| |
| print(f"当前路径 :>>>> {os.path.exists(file_path)}") |
(2)创建多级文件夹(makedirs)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| print(BASE_DIR) |
| |
| file_path = os.path.join(BASE_DIR, 'img', '4k') |
| print(file_path) |
| |
| if not os.path.exists(file_path): |
| |
| os.makedirs(file_path) |
| print(f"{file_path} :>>> 已创建") |
| |
| print(f"当前路径 :>>>> {os.path.exists(file_path)}") |
(3)删除单级文件夹(rmdir)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| file_name = 'img' |
| file_path = os.path.join(file_name, '4k') |
| print(file_path) |
| |
| file_true_path = os.path.join(BASE_DIR, file_path) |
| print(os.path.exists(file_true_path)) |
| |
| |
| os.rmdir(file_path) |
| |
| print(os.path.exists(file_true_path)) |
(4)删除多级文件夹(removedirs)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| file_path = os.path.join(BASE_DIR,'img','4k') |
| print(file_path) |
| |
| print(os.path.exists(file_path)) |
| |
| os.removedirs(file_path) |
| print(os.path.exists(file_path)) |
(5)列出当前文件夹下的所有文件及文件夹(listdir)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| file_list = os.listdir(BASE_DIR) |
| print(file_list) |
| |
(6)删除指定文件(remove)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| file_list_one = os.listdir(BASE_DIR) |
| print(file_list_one) |
| |
| |
| file_path = os.path.join(BASE_DIR, 'test.py') |
| os.remove(file_path) |
| |
| file_list_two = os.listdir(BASE_DIR) |
| print(file_list_two) |
| |
(7)重命名文件夹/文件(rename)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| file_list_one = os.listdir(BASE_DIR) |
| print(file_list_one) |
| |
| |
| file_path = os.path.join(BASE_DIR, 'test.py') |
| img_file_path = os.path.join(BASE_DIR, 'img') |
| os.rename(file_path, 'my_test.py') |
| os.rename(img_file_path, 'my_img') |
| |
| file_list_two = os.listdir(BASE_DIR) |
| print(file_list_two) |
| |
(8)获取当前工作目录(getcwd)
| import os |
| |
| |
| file_path = os.getcwd() |
| print(file_path) |
(9)改变当前工作目录(chdir)
| import os |
| |
| BASE_DIR = os.path.dirname(__file__) |
| |
| |
| file_path = os.getcwd() |
| print(file_path) |
| |
| |
| img_file_path = os.path.join(BASE_DIR, 'img') |
| os.chdir(img_file_path) |
| |
| file_path_new = os.getcwd() |
| print(file_path_new) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通