马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
一、导入路径Path函数
from pathlib import Path (另一种导入模块方法,这种导入方法,后面使用时就不需要加入模块名了。注意这里Path中的P要大写)
Path.cwd() (cwd获取当前目录的路径)
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312')
p=Path("C:/Users/Administrator/AppData/Local/Programs/Python/Python312") (把路径放到Path中,生成一个路径对象,放到p变量中)
p
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312')
q=p / "FishC.txt" (用“/”斜杠进行路径拼接)
q
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/FishC.txt')
二、路径查询的相关操作
(一)is_dir() 判断一个路径是否为一个文件夹
p.is_dir()
True
q.is_dir()
False
(二)is_file() 判断一个路径是否为一个文件
p.is_file()
False
q.is_file()
True
(三)exists()检测一个路径是否存在
p.exists()
True
q.exists()
True
Path("c:/404").exists()
False
(四)name 获取路径的最后一个部分(获取类函数后面不需要加小括号)
p.name
'Python312'
q.name
'FishC.txt'
(五)stem 获取文件名
q.stem
'FishC'
(六)suffix 获取文件后缀
q.suffix
'.txt'
(七)parent 获取父级目录路径
p.parent
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python')
q.parent
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312')
(八)parents (变成复数加了一个“s”,获取逻辑祖先路径构成的不可变序列)
p.parents
<WindowsPath.parents> (得到一个可迭代对象)
ps=p.parents (放到ps变量中)
for each in ps: (放入for中打印出来)
print(each)
C:\Users\Administrator\AppData\Local\Programs\Python
C:\Users\Administrator\AppData\Local\Programs
C:\Users\Administrator\AppData\Local
C:\Users\Administrator\AppData
C:\Users\Administrator
C:\Users
C:\
ps[0] (这个还支持索引,相当于减掉一级)
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python')
ps[1] (相当于减掉两级)
WindowsPath('C:/Users/Administrator/AppData/Local/Programs')
ps[2]
WindowsPath('C:/Users/Administrator/AppData/Local')
ps[3]
WindowsPath('C:/Users/Administrator/AppData')
ps[4]
WindowsPath('C:/Users/Administrator')
ps[5]
WindowsPath('C:/Users')
ps[6]
WindowsPath('C:/')
(九)parts 将路径的各个组件拆分成元组
p.parts
('C:\\', 'Users', 'Administrator', 'AppData', 'Local', 'Programs', 'Python', 'Python312')
(十)stat() 查询文件或文件夹的信息
p.stat() (这里有很多指定文件或文件夹的信息)
os.stat_result(st_mode=16895, st_ino=562949954041934, st_dev=3236480044419857018, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1710148581, st_mtime=1710115539, st_ctime=1705995213)
p.stat().st_size (这是指定文件或文件夹的尺寸,单位是字节)
4096 (4KB)
q.stat().st_size
29 (29个英文字符,29个字节,29B)
[color=Red]相对路径 VS 绝对路径
这里的P和q变量都代表的是绝对路径。
相对路径是以当前目录作为基准进行一级一级地目录推到的路径,使用“.”表示当前目录,使用“..”表示上一级目录。
Path("./doc")
WindowsPath('doc')
Path("../FishC")
WindowsPath('../FishC')
(十一)resolve() 将相对路径转换为绝对路径
Path("./doc").resolve()
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/Doc')
Path("../FishC").resolve()
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/FishC')
(十二)iterdir() 获取当前路径下所有子文件和子文件夹
p.iterdir()
<generator object Path.iterdir at 0x0000023D707E3780> (得到一个生成器)
for each in p.iterdir(): (放入for中打印出来)
print(each)
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\DLLs
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Doc
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\FishC.txt
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\include
……此次我省略12行打印内容。
如果想将当前路径下的所有文件整理成一个列表,不包含文件夹,就要加上一个筛选条件,如果筛选文件用is_file()函数,如果筛选文件夹用is_dir()函数)
[x for x in p.iterdir() if x.is_file()]
[WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/FishC.txt'), WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/LICENSE.txt'), ……此处省略7行内容。
以上都是路径查询的相关操作,那么修改路径操作也是支持的。
三、修改路径操作
(一)mkdir() 创建文件夹
n=p / "FishC" (用“/”进行路径拼接)
n.mkdir() (那么在C:/Users/Administrator/AppData/Local/Programs/Python/Python312/下面就创建了一个新的文件夹FishC)
n.mkdir() (再次创建同名的已存在的文件夹就会报错)
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
n.mkdir()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1311, in mkdir
os.mkdir(self, mode)
FileExistsError: [WinError 183] 当文件已存在时,无法创建该文件。: 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python312\\FishC'
n.mkdir(exist_ok=True) (加个参数exist_ok=True,回避避开报错信息,忽略报错信息)
如果路径中存在多个不存在的副级目录,有多个不存在得到副级目录,也会出错:
n=p / "FishC/A/B/C" (想在FishC文件下面第三级创建C文件夹,但是A和B目录根本不存在)
n.mkdir(exist_ok=True)
Traceback (most recent call last):
File "<pyshell#50>", line 1, in <module>
n.mkdir(exist_ok=True)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1311, in mkdir
os.mkdir(self, mode)
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python312\\FishC\\A\\B\\C'
n.mkdir(parents=True,exist_ok=True) (那就再加一个参数parents=True,就不报错了,而且把FishC文件下面三级目录A/B/C全部创建好,这样FishC下就创了多层文件夹A/B/C)
(二)open() 打开文件,Path的内部打包了一个open()方法,与上一节课的open()函数相比,Path中的这个open()第一个参数中不用写文件路径,其它与上一节的学习内容中的open()函数一样
n = n /"FishC.txt"
n
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/FishC/A/B/C/FishC.txt')
f=n.open("w") (不用写第一个文件路径参数)
f.write("I love little sister.") (写入一句字符串)
21
f.close() (需要用close()关闭后,再去找到文件双击打开查看,查看文件,发现上面的那句字符串被写入文件)
(三)rename() 修改文件或文件夹的名字
n.rename("NewFishC.txt") (由于把C:/*省略/Python312/FishC/A/B/C/FishC.txt的FishC.txt改名,C:/*省略/Python312/FishC/A/B/C/下的FishC.txt文件消失,但新文件NewFishC.txt并没有保存到C:/*省略/Python312/FishC/A/B/C/下,而是保存到Python的安装目录C:/*省略/Python312/下。原因是我们没有给新文件指定路径,程序就默认保存到Python的安装目录下。原因是rename()函数具有改名附带移动功能,带移动功能的还有一个replace()函数)
WindowsPath('NewFishC.txt')
(四)replace() 替换指定的文件或文件夹
m=Path("NewFishC.txt")
m
WindowsPath('NewFishC.txt')
n
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/FishC/A/B/C/FishC.txt') (这个地方有点不明白,前面用rename()改名且移动C:/*省略/Python312/FishC/A/B/C/下的FishC.txt的文件,按理说此处的FishC.txt应该消失了,事实上直接查看,和下面的n.stat().st_size查询它的大小信息也显示“系统找不到指定文件”,不知为什么这里执行n变量。显示的n竟然还是一个文件,而且是FishC.txt文件)
n.stat().st_size
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
n.stat().st_size
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 840, in stat
return os.stat(self, follow_symlinks=follow_symlinks)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python312\\FishC\\A\\B\\C\\FishC.txt'
m.replace(n) (这样Python安装目录下的NewFishC.txt消失,跑到C:/*省略/Python312/FishC/A/B/C/下,并且更名为FishC.txt)
WindowsPath('C:/Users/Administrator/AppData/Local/Programs/Python/Python312/FishC/A/B/C/FishC.txt')
n.stat().st_size (查询FishC.txt的尺寸,与原来NewFishC.txt尺寸一样大小)
21
(五)rmdir() 删除文件夹; unlink() 删除文件。
n.parent.rmdir() (报错,原因是文件夹里还有文件,不是空文件夹,不让删除)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
n.parent.rmdir()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\pathlib.py", line 1351, in rmdir
os.rmdir(self)
OSError: [WinError 145] 目录不是空的。: 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python312\\FishC\\A\\B\\C'
n.unlink() (先删除文件,让C目录成文空文件夹)
n.parent.rmdir() (再次删除C文件夹,进入C:/*省略/Python312/FishC/A/B/文件夹查看,文件夹C已经被删除)
(六)glob() 功能强大的查找功能
p=Path(".") (Python的安装目录,为当前目录,上面所有命令都是在当前目录下执行的命令)
p.glob("*.txt") (查询当前目录下所有后缀是.txt的文件,*号代表文件名)
<generator object Path.glob at 0x0000023D72BDA130>
list(p.glob("*.txt")) (list列出当前目录下所有后缀是.txt的文件)
[WindowsPath('FishC.txt'), WindowsPath('LICENSE.txt'), WindowsPath('NEWS.txt')]
list(p.glob("*/*.py")) (如果想查找当前目录下的下一级目录中的所有.py后缀的文件相当于当前目录的副一级中找.py后缀文件,不找副二级、副三级、……的后缀是.py的文件)
Squeezed text(64 lines) (共找到68行,这里可以双击展开看,也可右键copy,粘贴到其它地方查看)
list(p.glob("**/*.py")) (如果想进行向下的递归搜索(把当前目录、副一级、副二级、副三级……的所有子目录中都搜索完),也就是说查找当前目录以及该目录下的所有的子目录,可以用两“**”来表示
Squeezed text(1449 lines) (太多了,不要轻易双击展开,否则IDLE程序卡死。)
|