(python) os.path.exists os.path.isfile 谎言?
os.path.exists 给了我不正确的答案。
这与下面链接中讨论的问题不同,因为我在 Windows 上。 还有其他原因导致失败吗?
当我针对以下位置的文件进行测试时,测试返回正常与 *.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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
os.path.exists
和os.path.isfile
在 Windows 计算机中不区分大小写。这是我在 Windows 7 (Python 2.7) 中得到的结果
os.path.exists
andos.path.isfile
is not case sensitive in Windows machines.Here's what I get in Windows 7 (Python 2.7)
事实证明,os.path.exists 和 os.path.isfile 是区分大小写的..
废话!
Turned out, os.path.exists and os.path.isfile is case-sensitive..
Blah!