檢查appdata中是否存在文件而不在python代碼中添加完整路徑

瑪尼

如何使用環境變量檢查 appdata 中是否存在文件而不在 python 代碼中添加完整路徑?我添加了 %APPDATA% 但代碼不能使用環境變量

import os

PATH = '%APPDATA%\\java.exe'
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
    print("File exists and is readable")
else:
    print("Either the file is missing or not readable")

科拉連

嘗試使用os.path.expandvars

import os

PATH = os.path.expandvars('%APPDATA%\\java.exe')  # <- HERE
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
    print("File exists and is readable")
else:
    print("Either the file is missing or not readable")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章