Python quot;FileExistsquot; error when making directory(Python“文件存在制作目录时出错)
问题描述
我有几个线程在集群系统上从 Python 并行运行.每个 python 线程都输出到一个目录 mydir
.每个脚本,在输出之前检查 mydir 是否存在,如果不存在则创建它:
I have several threads running in parallel from Python on a cluster system. Each python thread outputs to a directory mydir
. Each script, before outputting checks if mydir exists and if not creates it:
if not os.path.isdir(mydir):
os.makedirs(mydir)
但这会产生错误:
os.makedirs(self.log_dir)
File "/usr/lib/python2.6/os.py", line 157, in makedirs
mkdir(name,mode)
OSError: [Errno 17] File exists
我怀疑这可能是由于竞争条件造成的,其中一个工作在另一个工作之前创建了 dir.这可能吗?如果是这样,如何避免这个错误?
I suspect it might be due to a race condition, where one job creates the dir before the other gets to it. Is this possible? If so, how can this error be avoided?
我不确定这是一个竞争条件,所以想知道 Python 中的其他问题是否会导致这个奇怪的错误.
I'm not sure it's a race condition so was wondering if other issues in Python can cause this odd error.
推荐答案
任何时间代码都可以在你检查某事和你采取行动之间执行,你将有一个竞争条件.避免这种情况的一种方法(以及 Python 中的常用方法)是尝试然后处理异常
Any time code can execute between when you check something and when you act on it, you will have a race condition. One way to avoid this (and the usual way in Python) is to just try and then handle the exception
while True:
mydir = next_dir_name()
try:
os.makedirs(mydir)
break
except OSError, e:
if e.errno != errno.EEXIST:
raise
# time.sleep might help here
pass
如果你有很多线程试图创建一系列可预测的目录,这仍然会引发很多异常,但你最终会到达那里.在这种情况下,最好只有一个线程创建目录
If you have a lot of threads trying to make a predictable series of directories this will still raise a lot of exceptions, but you will get there in the end. Better to just have one thread creating the dirs in that case
这篇关于Python“文件存在"制作目录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python“文件存在"制作目录时出错


基础教程推荐
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01