gpt4 book ai didi

Python - 运行文件夹树时出现 IOError

转载 作者:行者123 更新时间:2023-12-01 04:56:32 28 4
gpt4 key购买 nike

我正在尝试读取文件夹树中的一系列 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

最佳答案

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

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

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

为了解决您的问题..在阅读之前加入目录和文件名..例如

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

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

关于Python - 运行文件夹树时出现 IOError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27224053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com