os.makedirs不会在Windows上创建文件夹

问题描述 投票:0回答:1

我正在使用python 3.7和以下命令创建一个在Linux上运行而不在Windows上运行的目录:

       try:
        #shutil.rmtree('../../dist')
        os.makedirs('../../dist')
    except OSError as e:
        print("fffffffffffffffffffffffffffff")
        print(e)
        if e.errno != errno.EEXIST:
            raise

这是我在Windows上运行时遇到的错误:

fffffffffffffffffffffffffffff
[WinError 183] Cannot create a file when that file already exists: 
'../../dist'

并且根本没有dist文件夹,我不知道该错误是什么

任何的想法?

python windows python-3.x
1个回答
1
投票

根据OP的要求评论为答案:

这里的问题是你提供了一个相对于脚本的路径,但相对路径是相对于进程的工作目录解释的,这通常与脚本位置完全不同。该目录已经相对于工作目录存在,但您正在查看相对于脚本的路径,并且(正确地)在那里找不到任何内容。

如果必须相对于脚本创建目录,请将代码更改为:

scriptdir = os.path.dirname(__file__)
# abspath is just to simplify out the path so error messages are plainer
# while os.path.join ensures the path is constructed with OS preferred separators
newdir = os.path.abspath(os.path.join(scriptpath, '..', '..', 'dist'))
os.makedirs(newdir)
© www.soinside.com 2019 - 2024. All rights reserved.