【问题标题】:Accessing files within a parent folder and it's sub-folders using Python使用 Python 访问父文件夹及其子文件夹中的文件
【发布时间】:2014-02-09 23:45:50
【问题描述】:

我有一个文件夹,其中包含我想从 IDLE 读取并分析的 1000 张 DICOM 图像的子文件夹。

我已使用以下代码查找文件路径:

import sys           
print sys.path

我随后尝试将我想要访问的文件夹放在这些文件路径中,但是我仍然无法访问这些文件并且我收到以下错误:

>>> fp = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'IM-0268-0001.dcm' 

我也试过了:

sys.path.insert(0, 'C:/desktop/James_Phantom_CT_Dec_16th/Images')

但这对我也不起作用。非常感谢帮助,非常沮丧。

(使用 Python 2.7,64 位 windows 操作系统)。

【问题讨论】:

    标签: windows file python-2.7 path dicom


    【解决方案1】:

    打开文件时,Python 不会搜索路径。您必须指定打开的完整路径:

    d = 'C:/desktop/James_Phantom_CT_Dec_16th/Images'
    fp = open(d +'IM-0268-0001.dcm',"rb")

    编辑: d 是将保存路径的字符串,这样您就不必为每个文件重新键入它。 fp 将保存您将使用的文件对象。 “rb”是您要打开文件的方式:
    r - 读
    w - 用截断写
    a - 附加
    r+ - 读写
    此外,如果在 Windows 中工作,请添加“b”以处理二进制文件。见here

    【讨论】:

    • 你能解释一下'd'、'fp'、'fp +'和''rb''吗?
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2019-01-02
    相关资源
    最近更新 更多