代码之家  ›  专栏  ›  技术社区  ›  Hamed Minaee

os.makedirs公司不在windows上创建文件夹

  •  0
  • Hamed Minaee  · 技术社区  · 6 年前

    我正在使用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文件夹,我也不知道那是什么错误

    1 回复  |  直到 6 年前
        1
  •  1
  •   ShadowRanger    6 年前

    根据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)