如何使用環境變量檢查 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] 删除。
我来说两句