获取当前工作目录外的文件的python代码

2024-04-20 05:11:57 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python代码获取当前工作目录之外的文件

我正在使用e:\names\test.py directory

dirpath = os.path.dirname(os.path.realpath(__file__))
dirpath prints e:\names

e:\images

如何从文件test.py获取路径e:\images\imh.png 我在test.py中硬编码上述路径,如何在test.py文件中设置相对路径


Tags: 文件path代码pytest路径目录names
1条回答
网友
1楼 · 发布于 2024-04-20 05:11:57

您可以使用:

os.path.split(os.getcwd())[0]以获取父目录。在

然后,您可以使用:

a=os.path.split(os.getcwd())[0]
os.listdir(a)

用于列出父目录的内容

而且,这是有效的同时:在

^{pr2}$

相关问题 更多 >