pwd Path ----
时间: 2024-02-10 19:03:19 浏览: 30
`pwd`命令可以在终端中打印当前工作目录的路径。而`Path`是Python标准库中的一个模块,用于处理文件和目录路径。在Python代码中使用`Path`模块可以方便地进行文件和目录的操作,例如创建、修改、删除文件或目录,遍历目录等。以下是`Path`模块的一些常用操作示例:
1. 创建目录:
```
from pathlib import Path
path = Path('/path/to/directory')
path.mkdir()
```
上述代码将在`/path/to`目录下创建名为`directory`的子目录。
2. 创建文件并写入内容:
```
from pathlib import Path
path = Path('/path/to/file.txt')
path.write_text('hello world')
```
上述代码将在`/path/to`目录下创建名为`file.txt`的文件,并写入`hello world`的文本内容。
3. 遍历目录:
```
from pathlib import Path
path = Path('/path/to/directory')
for file in path.glob('*.txt'):
print(file)
```
上述代码将打印`/path/to/directory`目录下所有扩展名为`.txt`的文件路径。
4. 判断文件或目录是否存在:
```
from pathlib import Path
path = Path('/path/to/file.txt')
if path.exists():
print('file exists')
else:
print('file does not exist')
```
上述代码将判断`/path/to/file.txt`文件是否存在,并输出相应的提示信息。
希望这些示例可以帮助您更好地理解`Path`模块的使用。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)