ToB企服应用市场:ToB评测及商务社交产业平台

标题: Python中pathlib 模块的用法 [打印本页]

作者: 伤心客    时间: 2024-5-17 14:34
标题: Python中pathlib 模块的用法
pathlib 模块提供了表示文件系统路径的类,可适用于不同的操纵系统。
使用 pathlib 模块,相比于 os 模块可以写出更简洁,易读的代码。pathlib 模块中的 Path 类继承自 PurePath,对 PurePath 中的部分方法举行了重载,相比于 os.path 有更高的抽象级别。
本文将带你学习怎样使用 pathlib 模块中的 Path 类读写文件、操纵文件路径和基础文件系统,统计目录下的文件范例以及查找匹配目录下某一范例文件等。
下面就开始进入我们的学习时候。
1.获取目录

应用示例:
  1. from pathlib import Path
  2. currentPath = Path.cwd()
  3. homePath = Path.home()
  4. print("文件当前所在目录:%s\n用户主目录:%s" %(currentPath, homePath))
复制代码
2.目录拼接

斜杠 / 操纵符用于拼接路径,比如创建子路径。
应用示例:
  1. from pathlib import Path
  2. currentPath = Path.cwd()
  3. newPath = currentPath / 'python-100'
  4. print("新目录为:%s" %(newPath))
复制代码
3.创建、删除目录

应用示例:
  1. from pathlib import Path
  2. currentPath = Path.cwd()
  3. makePath = currentPath / 'python-100'
  4. makePath.mkdir()
  5. print("创建的目录为:%s" %(nmakePath))
  6. from pathlib import Path
  7. currentPath = Path.cwd()
  8. delPath = currentPath / 'python-100'
  9. delPath.rmdir()
  10. print("删除的目录为:%s" %(delPath))
复制代码
4.读写文件

应用示例:
  1. from pathlib import Path
  2. currentPath = Path.cwd()
  3. mkPath = currentPath / 'python-100.txt'
  4. with mkPath.open('w') as f:  # 创建并以 "w" 格式打开 python-100.txt 文件。
  5.     f.write('python-100')  # 写入 python-100 字符串。
  6. f = open(mkPath, 'r')
  7. print("读取的文件内容为:%s" % f.read())
  8. f.close()
  9. from pathlib import Path
  10. currentPath = Path.cwd()
  11. mkPathText = currentPath / 'python-100-text.txt'
  12. mkPathText.write_text('python-100')
  13. print("读取的文件内容为:%s" % mkPathText.read_text())
  14. str2byte = bytes('python-100', encoding = 'utf-8')
  15. mkPathByte = currentPath / 'python-100-byte.txt'
  16. mkPathByte.write_bytes(str2byte)
  17. print("读取的文件内容为:%s" % mkPathByte.read_bytes())
复制代码
5.获取文件所在目录的不同部分字段

  1. from pathlib import Path
  2. txtPath = Path('python-100.txt')
  3. nowPath = txtPath.resolve()
  4. print("文件的完整路径为:%s" % nowPath)
  5. print("文件完整名称为(文件名+后缀名):%s" % nowPath.name)
  6. print("文件名为:%s" % nowPath.stem)
  7. print("文件后缀名为:%s" % nowPath.suffix)
  8. print("文件所在的文件夹名为:%s" % nowPath.parent)
  9. print("文件所在的盘符为:%s" % nowPath.anchor)
复制代码
6.文件、路径是否存在判断

  1. from pathlib import Path
  2. currentPath = Path.cwd() / 'python'
  3. print(currentPath.exists())  # 判断是否存在 python 文件夹,此时返回 False。
  4. print(currentPath.is_dir())  # 判断是否存在 python 文件夹,此时返回 False。
  5. currentPath.mkdir()  # 创建 python 文件夹。
  6. print(currentPath.exists())  # 判断是否存在 python 文件夹,此时返回 True。
  7. print(currentPath.is_dir())  # 判断是否存在 python 文件夹,此时返回 True。
  8. currentPath = Path.cwd() / 'python-100.txt'
  9. print(currentPath.exists())  # 判断是否存在 python-100.txt 文件,此时文件未创建返回 False。
  10. print(currentPath.is_file())  # 判断是否存在 python-100.txt 文件,此时文件未创建返回 False。
  11. f = open(currentPath,'w')  # 创建 python-100.txt 文件。
  12. f.close()
  13. print(currentPath.exists())  # 判断是否存在 python-100.txt 文件,此时返回 True。
  14. print(currentPath.is_file())  # 判断是否存在 python-100.txt 文件,此时返回 True。
复制代码
7.文件统计以及匹配查找

  1. # 使用 Path.iterdir() 获取当前文件下的所有文件,并根据后缀名统计其个数。
  2. #学习中遇到问题没人解答?小编创建了一个Python学习交流群:153708845
  3. import pathlib
  4. from collections import Counter
  5. currentPath = pathlib.Path.cwd()
  6. gen = (i.suffix for i in currentPath.iterdir())
  7. print(Counter(gen))
  8. import pathlib
  9. from collections import Counter
  10. currentPath = pathlib.Path.cwd()
  11. gen = (i.suffix for i in currentPath.glob('*.txt'))  # 获取当前文件下的所有 txt 文件,并统计其个数。
  12. print(Counter(gen))
  13. gen = (i.suffix for i in currentPath.rglob('*.txt'))  # 获取目录中子文件夹下的所有 txt 文件,并统计其个数。
  14. print(Counter(gen))
复制代码
8.总结

本文给各人先容了 Python 的 pathlib 模块,为 Python 工程师对该模块的使用提供了支持,让各人了解怎样使用 pathlib 模块读写文件、操纵文件路径和基础文件系统,统计目录下的文件范例以及查找匹配目录下某一范例文件等。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4