<bdo id='1V3Y8'></bdo><ul id='1V3Y8'></ul>
    <tfoot id='1V3Y8'></tfoot>

        <small id='1V3Y8'></small><noframes id='1V3Y8'>

      1. <legend id='1V3Y8'><style id='1V3Y8'><dir id='1V3Y8'><q id='1V3Y8'></q></dir></style></legend>
      2. <i id='1V3Y8'><tr id='1V3Y8'><dt id='1V3Y8'><q id='1V3Y8'><span id='1V3Y8'><b id='1V3Y8'><form id='1V3Y8'><ins id='1V3Y8'></ins><ul id='1V3Y8'></ul><sub id='1V3Y8'></sub></form><legend id='1V3Y8'></legend><bdo id='1V3Y8'><pre id='1V3Y8'><center id='1V3Y8'></center></pre></bdo></b><th id='1V3Y8'></th></span></q></dt></tr></i><div id='1V3Y8'><tfoot id='1V3Y8'></tfoot><dl id='1V3Y8'><fieldset id='1V3Y8'></fieldset></dl></div>

      3. Python“文件存在"制作目录时出错

        Python quot;FileExistsquot; error when making directory(Python“文件存在制作目录时出错)
        • <small id='DqSo9'></small><noframes id='DqSo9'>

            <tbody id='DqSo9'></tbody>

              <legend id='DqSo9'><style id='DqSo9'><dir id='DqSo9'><q id='DqSo9'></q></dir></style></legend>

                <i id='DqSo9'><tr id='DqSo9'><dt id='DqSo9'><q id='DqSo9'><span id='DqSo9'><b id='DqSo9'><form id='DqSo9'><ins id='DqSo9'></ins><ul id='DqSo9'></ul><sub id='DqSo9'></sub></form><legend id='DqSo9'></legend><bdo id='DqSo9'><pre id='DqSo9'><center id='DqSo9'></center></pre></bdo></b><th id='DqSo9'></th></span></q></dt></tr></i><div id='DqSo9'><tfoot id='DqSo9'></tfoot><dl id='DqSo9'><fieldset id='DqSo9'></fieldset></dl></div>
                <tfoot id='DqSo9'></tfoot>
                  <bdo id='DqSo9'></bdo><ul id='DqSo9'></ul>
                  本文介绍了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“文件存在"制作目录时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                • <i id='OxTsm'><tr id='OxTsm'><dt id='OxTsm'><q id='OxTsm'><span id='OxTsm'><b id='OxTsm'><form id='OxTsm'><ins id='OxTsm'></ins><ul id='OxTsm'></ul><sub id='OxTsm'></sub></form><legend id='OxTsm'></legend><bdo id='OxTsm'><pre id='OxTsm'><center id='OxTsm'></center></pre></bdo></b><th id='OxTsm'></th></span></q></dt></tr></i><div id='OxTsm'><tfoot id='OxTsm'></tfoot><dl id='OxTsm'><fieldset id='OxTsm'></fieldset></dl></div>
                  <legend id='OxTsm'><style id='OxTsm'><dir id='OxTsm'><q id='OxTsm'></q></dir></style></legend>

                        <tbody id='OxTsm'></tbody>

                          • <bdo id='OxTsm'></bdo><ul id='OxTsm'></ul>

                            <small id='OxTsm'></small><noframes id='OxTsm'>

                            <tfoot id='OxTsm'></tfoot>