python目录操作函数_python中如何一次输入多个

python目录操作函数_python中如何一次输入多个在 Python 中获取当前工作目录的路径 可以使用 os getcwd 函数或者 pathlib Path cwd 方法 以下是两种方法的示例 1 使用 os getcwd pythonimport oscurrent dir os getcwd print 当前目录是 current dir 2 使用 Path cwd

在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)

编程小号
上一篇 2026-04-14 12:04
下一篇 2026-04-14 11:56

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/57704.html