【问题标题】:Python - IOError when running through folder treePython - 通过文件夹树运行时出现 IOError
【发布时间】:2014-12-01 08:04:03
【问题描述】:

我正在尝试读取文件夹树中的一系列 DICOM 文件,并且我正在使用以下代码在树中运行,同时读取每个文件。问题是我得到了肯定存在的文件的 IOErrors,我已经检查了文件权限和其他 SO 线程,例如Python: IOError: [Errno 2] No such file or directory,但是如果没有这些 IOErrors,我还没有设法让它工作。有人有什么想法吗?

for root, dirs, files in os.walk(path):
      for fname in files:
        name = os.path.basename(os.path.abspath(fname))
        if name.startswith('.') == True:
          pass
        else:
          try:
            plan=dicom.read_file(fname)
            ds=dicom.read_file(fname, stop_before_pixels = True)

            kVp = TagChecker([0x18,0x60]) #KVP
            Target =  TagChecker([0x18,0x1191]) #ANODE
            Filter =  TagChecker([0x18,0x7050]) #

            write_results.writerow([Survey_Number, Patient_ID, View_Protocol, int(kVp), Target, Filter, Thickness, mAs_Exposure, LPad_Yes_No, autoorman, AECMode, AECDset, Patient_Age, Comment, Compression_Force])
            #print(fname)
          except IOError:
            print "IOError: ", "//" + os.path.join(root, fname) + "//"
          except InvalidDicomError:
          # This exception line prints an error message to the command line, checks to see if an error log
          # has been generated for this session, writes a new one if not and then writes the error to the log file
            print "Invalid Dicom File: ", fname

【问题讨论】:

  • 我已经把它缩小到我在文件夹中循环编码的方式上美好的。有人可以帮忙吗?

标签: python dicom ioerror


【解决方案1】:

通常采用文件名的方法,如 dicom.read_file(fname),将采用绝对文件名(或假设文件名相对于您的主要 python 程序运行的目录,cwd())。我可以建议您将此行放在第一个 read_file() 调用之前吗:

print "reading: %s" % os.path.abspath(fname)

然后您将看到您实际尝试读取的文件名。我猜这不是您认为要查找的文件(或机器人)。

为了解决您的问题.. 在阅读之前加入 dir 和 fname.. 例如

full_fname = os.path.join(dir, fname)
dicom.read_file(full_fname)

换句话说,我认为您正在读取具有相对路径的文件并且您希望使用绝对路径。

【讨论】:

  • 谢谢!它将文件名与当前目录路径连接起来,即脚本存储的路径而不是文件存储的路径,现在全部修复!作为参考,这只是添加以下内容的简单案例: full_fname = os.path.join(root, fname)
猜你喜欢
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-09
  • 2015-09-01
  • 2022-06-14
  • 2011-11-22
  • 2012-08-05
相关资源
最近更新 更多