(python) os.path.exists os.path.isfile 谎言?

发布于 2024-10-09 11:20:22 字数 997 浏览 0 评论 0原文

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

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

os.path.exists() 谎言

当我针对以下位置的文件进行测试时,测试返回正常与 *.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

os.path.exists is giving me incorrect answers.

it's not the same problem discussed at below link since I'm at windows.
Are there other reasons for it to fail?

os.path.exists() lies

The test returns ok when I test it against a file at the same directory as the *.py script runs, but none of its sub directories..

-EDIT-

I'm using absolute path.

I'm looking at one of the sub directories as this script runs, and can literally see file's last modified time field being changed in the windows explorer.
There are no other stuff going on my computer I can think of that will modify the files in question.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-10-16 11:20:22

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

os.path.exists and os.path.isfile is not case sensitive in Windows machines.

Here's what I get in 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
我要还你自由 2024-10-16 11:20:22

事实证明,os.path.exists 和 os.path.isfile 是区分大小写的..

废话!

Turned out, os.path.exists and os.path.isfile is case-sensitive..

Blah!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文