gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:26:01 25 4
gpt4 key购买 nike

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

正在使用e:\names\test.py 目录

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

e:\images

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

最佳答案

您可以使用:

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

然后,您可以使用:

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

用于列出父目录的内容

此外,这也有效:-

os.listdir(os.pardir)

关于python代码获取当前工作目录之外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com