即使 Windows 10 上不存在文件,os.path.exists也会返回True

Jer*_*ov2 6 python windows io file python-3.x

我在 Windows 10 中遇到一个奇怪的文件问题。我编写了一个 Python 程序,它将一些数据写入位于C:\Users\jerfo\AppData\Local\xmr-haystack\xmr-haystack\Cache. 我能够调用os.makedirs路径并写入目录中的文件,而不会引发任何错误。os.path.exists返回True此路径。但是,当我尝试在文件资源管理器中查看文件时,它们不在那里。我导航到C:\Users\jerfo\AppData\Local\,但找不到该xmr-haystack目录。我通过尝试运行命令来确认这一点cd C:\Users\jerfo\AppData\Local\xmr-haystack\xmr-haystack\Cache,但失败了。我已经重现这个问题近10次了,没有明显的变化。我见过很多人有相反的问题(即不应该返回时os.path.exists返回False),但我一直无法找到与我类似的问题的实例。大家对于导致这个问题的原因有什么见解吗?谢谢你!

Python版本:3.8.6

操作系统:Windows 10 家庭版 1909

有问题的代码:

import os

cache_dir = 'C:\\Users\\jerfo\\AppData\\Local\\xmr-haystack\\xmr-haystack\\Cache'
file_path = os.path.join(cache_dir, 'cache.json')

os.makedirs(cache_dir, exist_ok=True) # No errors
f = open(file_path, 'w') # No errors
f.write('hello') # No errors
f.close() # Still no errors

print(os.path.exists(cache_dir)) # Prints True, yet the folder doesn't exist
print(os.path.exists(file_path)) # Prints True, yet the file doesn't exist
Run Code Online (Sandbox Code Playgroud)

显示正在运行的代码的视频链接

链接到视频中相同代码演示的屏幕截图(如果不可用)。