python-使用os库来操作文件路径、文件信息、系统信息、目录等
2020-06-08 04:25
351 查看
在实际项目中,我们经常需要获取文件信息,对文件/目录进行操作等,而os模块则提供相关的功能,记录一下常用的方法
- 使用环境
- python版本:python3.7
- 操作系统:windows10
一、os:系统操作
方法 | 说明 | Windows | Linux/Unix |
---|---|---|---|
os.getcwd() | 返回当前文件的工作目录 | ||
os.name() | 返回所在工作平台对应的类型值 | nt | posix |
os.sep() | 返回对应系统路径的分隔符 | \ | / |
os.getenv(name) | 返回指定环境变量的变量值 | ||
os.get_terminal_size() | 返回控制台的长宽 |
实例:
#!/usr/bin/python # -*- coding: utf-8 -*- import os print(os.getcwd()) print(os.name) print(os.sep) print(os.getenv("CLASSPATH"))
输出结果:
二、os.path:操作路径相关
返回路径类
方法 | 说明 |
---|---|
os.path.abspath(path) | 返回绝对路径(包含文件名) |
os.path.basename(path) | 返回目录/文件的名字 |
os.path.dirname(path) | 返回目录/文件的路径 |
os.path.realpath(path) | 返回path的真实路径 |
os.path.commonprefix(list) | 返回list(多个路径)中,所有path共有的最长的路径(部分场景不合适) |
os.path.relpath(path[, start]) | 从start开始计算相对路径 |
实例:
#!/usr/bin/python # -*- coding: utf-8 -*- import os print('__file__的输出结果:' + __file__) # python内置函数,当前文件的路径 print(r'os.path.abspath(' + __file__ + ') -> ' + os.path.abspath(__file__)) print(r'os.path.basename(' + __file__ + ') -> ' + os.path.basename(__file__)) print(r'os.path.dirname(' + __file__ + ') -> ' + os.path.dirname(__file__)) print(os.path.commonprefix([r'E:\PyProject\Test_Pratice', r'E:\PyProject', r'E:\PyProject\Hardware_Devices_Interface', r'E:\PyProject\Auto_QRCode'])) print(os.path.commonprefix([r'E:\Py测试Project\Test_Pratice', r'E:\PyProject', r'E:\PyProject\Hardware_Devices_Interface', r'E:\PyProject\Auto_QRCode'])) print(os.path.relpath(__file__, start='test_os'))
输出结果
合成路径/切割路径类
方法 | 说明 |
---|---|
os.path.join(path1[, path2[, …]]) | 目录和文件名合成一个路径 |
os.path.split(path) | 分割路径为 dirname 和 basename,返回一个元组 |
os.path.splitdrive(path) | 分割路径为 驱动器名 和 路径,返回一个元组 |
os.path.splitext(path) | 分割路径为 路径名 和 文件扩展名,返回一个元组 |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os print('__file__的输出结果:' + __file__) # python内置函数,当前文件的路径 print(os.path.join(r'E:\PyProject\Test_Pratice', 'dir1', 'dir2', 'file.txt')) print(os.path.split(__file__)) print(os.path.splitdrive(__file__)) print(os.path.splitext(__file__))
输出结果
返回文件时间/文件大小类
方法 | 说明 |
---|---|
os.path.getatime(path) | 返回文件/目录最近访问时间 |
os.path.getmtime(path) | 返回最近文件/目录修改时间 |
os.path.getctime(path) | 返回文件/目录创建时间 |
os.path.getsize(path) | 返回文件/目录大小(字节),如果文件/目录不存在就返回错误 |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os import time print(os.path.getatime(__file__)) print(os.path.getmtime(__file__)) print(os.path.getctime(__file__)) print(os.path.getsize(__file__)) # 将时间戳转换下格式 print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getctime(__file__))))
输出结果
判断路径类
方法 | 说明 | 是 | 否 |
---|---|---|---|
os.path.isabs(path) | 判断是否为绝对路径 | True | False |
os.path.isfile(path) | 判断路径是否为文件 | True | False |
os.path.isdir(path) | 判断路径是否为目录 | True | False |
os.path.islink(path) | 判断路径是否为符号链接(低于Windows6.0的始终返回false。) | True | False |
os.path.ismount(path) | 判断路径是否为挂载点 | True | False |
os.path.exists(path) | 判断路径是否存在 | True | False |
os.path.lexists(path) | 判断路径是否存在,若路径损坏也返回True | True | False |
os.path.samefile(path1, path2) | 判断目录或文件是否相同,若目录/文件不存在返回错误 | True | False |
os.path.sameopenfile(fp1, fp2) | 判断fp1和fp2是否指向同一文件,若目录/文件不存在返回错误 | True | False |
os.path.samestat(stat1, stat2) | 判断stat tuple stat1和stat2是否指向同一个文件 | True | False |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os print('__file__的输出结果:' + __file__) # python内置函数,当前文件的路径 print(os.path.isabs(__file__)) print(os.path.isabs('../test_os.py')) # 相对路径 print(os.path.isfile(__file__)) print(os.path.isfile('E:/PyProject/Test_Pratice/')) print(os.path.isdir(__file__)) print(os.path.isdir('E:/PyProject/Test_Pratice/')) print(os.path.islink(__file__)) print(os.path.islink(r"E:\链接文件.txt")) print(os.path.ismount('E:/PyProject/Test_Pratice/'))
输出结果
规范路径类
方法 | 说明 |
---|---|
os.path.normcase(path) | 规范化路径名的大小写。使所有字符小写,并将所有斜杠改为反斜杠。 |
os.path.normpath(path) | 规范化路径,消除双斜杠等。 |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os print('__file__的输出结果:' + __file__) # python内置函数,当前文件的路径 print(os.path.normcase(__file__)) print(os.path.normpath('E:///PyProject////////Test_Pratice'))
输出结果
转换路径类
方法 | 说明 |
---|---|
os.path.expanduser(path) | 把path中包含的"~"和"user"转换成用户目录 |
os.path.expandvars(path) | 根据环境变量的值替换path中包含的"$name"和"${name}" |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os print('__file__的输出结果:' + __file__) # python内置函数,当前文件的路径 print(os.path.expanduser(r'~\Documents')) print(os.path.expandvars(r'%ProgramFiles%\WindowsPowerShell\Modules')) print(os.path.expandvars(r'.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar'))
输出结果
三、os-操作目录
目录增删改查
方法 | 说明 |
---|---|
os.listdir(path) | 返回path指定的文件夹包含的文件或文件夹的名字的列表。 |
os.mkdir(path) | 创建一个目录 |
os.rmdir(path) | 删除空目录,若目录有文件,返回错误 |
os.makedirs(path) | 创建多个目录 |
os.removedirs(path) | 删除多个空目录,若目录有文件,返回错误 |
os.chdir(path) | 将当前文件的工作目录更改为指定的路径 |
os.rename(src, dst) | 重命名文件或目录,执行的时候先把文件/目录关掉 |
os.renames(old, new) | 递归地对目录或文件进行更名 |
实例
#!/usr/bin/python # -*- coding: utf-8 -*- import os # 创建一个目录 os.mkdir('E:/PyProject/Test_Pratice/makedir/') # 删除空目录 os.rmdir('E:/PyProject/Test_Pratice/makedir') # 创建多个目录 os.makedirs('E:/PyProject/Test_Pratice/make_dir1/make_dir2/make_dir3') # 重命名单个目录 os.rename(src='E:/PyProject/Test_Pratice/make_dir1/', dst='E:/PyProject/Test_Pratice/make_dir1_rename/') # 重命名多个目录 os.renames(old='E:/PyProject/Test_Pratice/make_dir1_rename/make_dir2', new='E:/PyProject/Test_Pratice/make_dir1/make_dir2_rename') # 改变当前文件的工作目录到指定路径 os.chdir('E:/PyProject/Test_Pratice/make_dir1/make_dir2_rename/make_dir3') print(os.getcwd()) # 打印变更后的工作路径 # 删除多个目录 os.removedirs('E:/PyProject/Test_Pratice/make_dir1/make_dir2_rename/make_dir3') # 返回指定文件夹的文件/目录列表 print(os.listdir('E:/PyProject/Test_Pratice/')) #
输出结果
本章先记录到这里,后续有用到其他的功能再补充。
若有不对的地方,希望大家指正出来!
相关文章推荐
- python,os操作文件,文件路径(上一级目录)
- python,os操作文件,文件路径(上一级目录)
- Python使用os模块和fileinput模块来操作文件目录
- Python获取当前文件的绝对路径,使用os.path来得到当前操作文件的绝对路径
- python---os,sys的文件操作,strip()去除空格,.号当前目录查找,绝对路径查找
- Python使用os.listdir()和os.walk()获取文件路径与文件下所有目录的方法
- python os.statvfs() 查看文件系统信息 磁盘使用情况
- Python Os模块--路径、文件、系统命令等操作
- Python os模块--路径、文件、系统命令等操作
- Python:使用 os、shutil 模块对文件与目录操作
- python 常用对linux系统文件及目录的操作
- Python基础总结之第九天开始【python之OS模块对目录的操作、以及操作文件】(新手可相互督促)
- C# 对包含文件或目录路径信息的 System.String 实例执行操作
- python——【转载】os操作文件目录
- Python常用模块之os.path——文件及路径操作
- python os目录和文件相关操作
- node.js 使用fs模块对系统文件及目录进行读写操作
- Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)
- 手把手教你学python3第八讲(OS模块有关文件系统的函数操作)
- python OS模块(操作文件、目录)