Python Pathlib 模块

2024 年 8 月 29 日 | 14 分钟阅读

出于各种原因,与文件系统进行交互和处理文件至关重要。最简单的情况可能只是读取或写入文件,但有时也需要更复杂的操作。也许您需要显示某个特定类型的所有文件,查找文件的父目录,或者生成一个不存在的新文件名。

任何 Python 脚本都离不开文件系统。它可能像将数据文件读入 pandas DataFrame 一样简单,也可能像处理高度嵌套目录结构中的数百个文件一样复杂。为了实现这些目的,Python 的标准库提供了许多有用的方法,特别是 pathlib 模块。

Pathlib 模块最初出现在 Python 3.4 中,并在后续的每个版本中都得到了改进。Pathlib 是一个面向对象的 **文件系统接口**,它提供了一种更自然、与平台无关且更 Pythonic 的方式来与文件系统进行交互。我最近完成了一个简短的项目,在这个项目中,我选择使用 pathlib 和 pandas 来处理嵌套目录结构中数千个文件的排序和管理。这个项目启发了我写这篇文章。当我完成这一切时,我被 pathlib 的功能所震惊,我将来毫不犹豫地在项目中使用它。所有大于或等于 3.4 的 Python 版本都包含 pathlib 库。为了获得所有最新的升级,我建议使用最新版本的 Python。在这篇文章中,我将使用 Python 3.6。

Pathlib 模块的一个优点是,无需使用 os.joindir 即可轻松创建路径。例如,当我开始小型项目时,我使用 os.getcwd() 在当前工作目录下方创建输入和输出子目录。我将工作输入和输出文件保存在这些位置。代码可能如下所示:

语法

传统上,Python 一直使用普通文本字符串来表示文件路径。借助 os.path 标准库(如引言中的第二个示例所示),这已经足够了,尽管有些繁琐。然而,由于路径不是字符串,关键功能分散在标准库中,包括 os、glob 和 shutil 等库。为了将所有文本文件移动到存档目录,以下示例需要三个导入语句:

语法

使用字符串表示的路径,用标准字符串方法是可能的,但通常是一个糟糕的主意。您应该使用 os.path.join() 来连接两个路径,而不是像使用常规字符串那样使用 +,它使用操作系统正确的路径分隔符来连接路径。请记住,在 Windows 上,分隔符是 /,而在 Mac 和 Linux 上,分隔符是 /。这个区别可能会导致难以检测的问题,就像引言中的第一个示例一样,它只适用于 Windows 路径。

为了解决这些问题,Pathlib 模块在 Python 3.4 (PEP 428) 中被添加。它将所有必需的功能集中在一个地方,并通过简单的 Path 对象的方法和属性来访问。其他包最初使用字符串作为文件路径,但自 Python 3.6 以来,得益于文件系统路径协议的创建,pathlib 模块已在整个标准库中得到支持。如果您还在使用旧版 Python,也可以获取 Python 2 的向后移植版本。

代码

输出

root@superbook:/# python3 /home/nirnay/code.py 
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
1
>Enter the absolute path of the directory whose sub-directories you want to list::
/directory1
/directory1/directory4
/directory1/directory3
/directory1/directory2
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
2
>Enter the absolute path of the directory whose files you want to list::
/directory1
/directory1/java_code.java
/directory1/c++_code.c++
/directory1/python_code.py
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
3
>Enter the absolute path of the file which you want to check is present::
/directory1/python_code.py
The file is present.
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
4
The current working directory is: /
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
5
>Enter the absolute path to the new directory to which you want to change::
/directory1
The Current working directory changed successfully from / to /directory1
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
4
The current working directory is: /directory1
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
1
>Enter the absolute path of the directory whose sub-directories you want to list::
/directory1
/directory1/directory4
/directory1/directory3
/directory1/directory2
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
5
>Enter the absolute path to the new directory to which you want to change::
/directory1/directory2
The Current working directory changed successfully from /directory1 to /directory1/directory2
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
4
The current working directory is: /directory1/directory2
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
2
>Enter the absolute path of the directory whose files you want to list::
/directory1/directory2
/directory1/directory2/files6
/directory1/directory2/files1
/directory1/directory2/files3
/directory1/directory2/files4
/directory1/directory2/files2
/directory1/directory2/files5
/directory1/directory2/files8
/directory1/directory2/files10
/directory1/directory2/files7
/directory1/directory2/files9
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
5
>Enter the absolute path to the new directory to which you want to change::
/directory1/directory4
The Current working directory changed successfully from /directory1/directory2 to /directory1/directory4
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
5
>Enter the absolute path to the new directory to which you want to change::
/directory1/directory3
The Current working directory changed successfully from /directory1/directory4 to /directory1/directory3
Do you want to continue or exit the code execution?[y/n]
y
Please choose one of the appropriate options::
1. To print all the sub-directories of a folder/dir.
2. To print all the files present inside a folder/directory.
3. Check the existence of a specified file
4. To check the current directory of the code execution.
5. To modify the current directory of the code execution. (using the chdir() function)
6. To exit from the code execution.
4
The current working directory is: /directory1/directory3
Do you want to continue or exit the code execution?[y/n]
n
root@superbook:/# tree directory1

directory1/
├── c++_code.c++
├── directory2
│   ├── files1
│   ├── files10
│   ├── files2
│   ├── files3
│   ├── files4
│   ├── files5
│   ├── files6
│   ├── files7
│   ├── files8
│   └── files9
├── directory3
│   ├── files11
│   ├── files12
│   ├── files13
│   ├── files14
│   ├── files15
│   ├── files16
│   ├── files17
│   ├── files18
│   ├── files19
│   └── files20
├── directory4
│   ├── files21
│   ├── files22
│   ├── files23
│   ├── files24
│   ├── files25
│   ├── files26
│   ├── files27
│   ├── files28
│   ├── files29
│   └── files30
├── java_code.java
└── python_code.py

说明

在查看上述代码的输出之前,目录结构显示父目录名为 directory 1,其下有三个子目录,分别名为 directory 2、directory 3 和 directory 4。每个子目录中都存在 10 个文件,而在父目录中,除了这三个子目录之外,还有三个文件。

现在让我们看看上述代码的输出,在执行上述程序后,用户会看到 6 个选项:第一个选项是列出父目录下的所有子目录;第二个选项是列出特定目录下的所有文件;选项 3 是检查特定文件是否在特定位置存在;选项 4 是打印代码执行的当前工作目录;选项 5 是将代码执行的当前工作目录更改为指定为该功能方法参数的新目录。最后是第 6 个选项,用于执行代码。

结论

因此,在本文中,我们了解了 Python 的 pathlib 模块,并看到了可以利用 pathlib 模块提供的各种功能的各种用例场景。