【问题标题】:(python) os.path.exists os.path.isfile lies?(python) os.path.exists os.path.isfile 谎言?
【发布时间】:2010-12-30 19:31:24
【问题描述】:

os.path.exists 给了我不正确的答案。

这与下面链接中讨论的问题不同,因为我在 Windows 上。 还有其他失败的原因吗?

os.path.exists() lies

当我针对与 *.py 脚本运行的同一目录中的文件进行测试时,测试返回正常,但没有任何子目录..

-编辑-

我使用的是绝对路径。

当此脚本运行时,我正在查看其中一个子目录,并且可以从字面上看到文件的最后修改时间字段在 Windows 资源管理器中被更改。
我的电脑上没有其他东西可以修改相关文件。

def SaveIfNewer(doc, aiFile, pngFile):
    options = win32com.client.Dispatch('Illustrator.ExportOptionsPNG24')
    options.SetArtBoardClipping(True)
    if (os.path.exists(pngFile)):
        aiFileTime = os.stat(aiFile)[8]
        pngFileTime = os.stat(pngFile)[8]
        print("aiFileTime: ", aiFileTime, "pngFileTime: ", pngFileTime)

        if(aiFileTime > pngFileTime):
            os.remove(pngFile)

    if( not os.path.isfile(pngFile)):
        doc.Export(pngFile, constants.aiPNG24, options)
        print 'exporting:', pngFile
    else:
        print 'skipping file:', pngFile

【问题讨论】:

  • 这些不正确的答案是什么?是什么让您的问题与链接中的问题不同?您能向我们展示您的代码和示例目录布局吗?
  • 您使用的是绝对路径还是相对路径?如果是相对的,请检查 getcwd() 的返回

标签: python windows path


【解决方案1】:

os.path.existsos.path.isfile 在 Windows 机器中不区分大小写。

这是我在 Windows 7 (Python 2.7) 中得到的结果

>>> os.path.exists('C:/.rnd')
True
>>> os.path.exists('C:/.RND')
True
>>> os.path.isfile('C:/.rnd')
True
>>> os.path.isfile('C:/.RND')
True

【讨论】:

  • 可能是不同的版本,我对python-dev上讨论的这个有一些模糊的记忆。
  • 我用 c:\a\B\c.txt 和 c:\a\b\c.txt 测试,结果不同
【解决方案2】:

原来,os.path.exists 和 os.path.isfile 是区分大小写的..

废话!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多