python - os.path.exists 导致文件被删除
问题描述
在为 python 使用Audacity脚本环境时,我遇到了以下问题。它创建一个临时文件,可以在其中编写要由 Audacity 执行的命令。Audacity 启动时正在创建两个相关文件。
我的脚本包含以下片段(原始来源):
# Initialize variable depending on os:
if sys.platform == 'win32':
print("pipe-test.py, running on windows")
TONAME = '\\\\.\\pipe\\ToSrvPipe'
FROMNAME = '\\\\.\\pipe\\FromSrvPipe'
# Check if temp files actually exist
if not os.path.exists(TONAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
到目前为止一切都很好,找到了文件。然后我尝试打开,每次都操作失败。我在这里和那里做了一些调整,但最终发现这种奇怪的行为表明打开文件不是问题,而是exists()
检查。我像这样更改了我的代码:
# Initialize variable depending on os:
[...]
# Check if temp files actually exist (it exists)
if not os.path.exists(TONAME):
print(" ..does not exist. Ensure Audacity is running with mod-script-pipe.")
sys.exit()
# Check if temp files actually exist (it is gone)
if not os.path.exists(TONAME):
print("File is gone now")
sys.exit()
第二次检查会告诉我,在我检查文件是否存在后,该文件突然消失了。这怎么可能?怎么可能os.path.exists()
删除了我的临时文件?
解决方案
推荐阅读
- php - 基于组的自动增量
- php - yii框架中的数据库迁移错误
- reactjs - 无法更改蚂蚁设计的 OptGroup 的样式
- sqlite - Sqlite 获取 DateTime 的日期部分并返回为 DateTime,而不是字符串
- javascript - 将动态添加的 html 列表项推送到最后一个数组中
- go - 从 go 通道流中读取
- python-3.x - 将 JSON 转换为 Excel 表格
- javascript - 如何修复 body-parser 不解析 Express NodeJS 中的 html 表单日期输入字段?
- javascript - 如何使 html5 输入使用模式属性仅接受来自 gmail.com 的电子邮件地址?
- c++ - 具有默认类的模板参数列表