python - os.makedirs 不在 Windows 上创建文件夹
问题描述
我正在使用 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 文件夹,我不知道那个错误是什么
任何的想法?
解决方案
根据 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)
推荐阅读
- python - Sqlalchemy 如何查询 citext 数组
- audio - 从 MP4 创建 M4A 文件
- python-3.x - 与数组组合时列表索引超出范围?
- javascript - 如何检查数组中的每个元素的特定条件
- vhdl - VHDL fullAdder 模拟?
- woocommerce - 在结帐时移动 WooCommerce 密码字段
- tensorflow - 使用对象检测 tensorflow API 在录制的视频中进行预测
- javascript - 无法在 Angular 中使用子组件的选择器
- electron - Electron:如何检测应用程序是否处于未聚焦/后台
- html - 如何使用 Bulma 完成像 Google Keep 这样的砌体布局?