在Python中获取当前工作目录的路径,可以使用`os.getcwd()`函数或者`pathlib.Path.cwd()`方法。以下是两种方法的示例:
1. 使用`os.getcwd()`:
```python
import os
current_dir = os.getcwd()
print("当前目录是", current_dir)
2. 使用`Path.cwd()`:
```python
from pathlib import Path
current_dir = Path.cwd()
print("当前目录是", current_dir)
两者都可以返回当前工作目录的路径,但`pathlib`是Python 3.4及以后版本引入的新库,提供了面向对象的文件系统路径操作接口,更加直观和方便。
如果你需要获取目录中的内容,可以使用`os.listdir()`函数:
```python
import os
files_in_dir = os.listdir()
print("目录中的内容:", files_in_dir)