[英]How to get a file using %APPDATA% in python 3.9?
I want to store in a variable this path APPDATA%/Roaming/FileZilla/sitemanager.xml
.我想将此路径存储在变量中APPDATA%/Roaming/FileZilla/sitemanager.xml
。
When i use:当我使用:
file = "C:/Users/MyPC/AppData/Roaming/FileZilla/sitemanager.xml"
it works, but when i use:它有效,但是当我使用时:
file = "%APPDATA%/Roaming/FileZilla/sitemanager.xml"
the file cannot be stored and i cant send via paramiko.该文件无法存储,我无法通过 paramiko 发送。
Anyone helps?有人帮忙吗?
You can retrieve the env variable with getenv
function from os
module.您可以使用getenv
function 从os
模块检索环境变量。 You can also use Path
to easily manipulate paths.您还可以使用Path
轻松操作路径。
import os
import pathlib
appdata = pathlib.Path(os.getenv('APPDATA'))
xmlfile = appdata / 'FileZilla' / 'sitemanager.xml'
You can use os.path.expandvars
to expand environment variables in a string.您可以使用os.path.expandvars
来扩展字符串中的环境变量。 On Windows, $ and % forms are accepted.在 Windows 上,接受 $ 和 % forms。
import os
file = os.path.expandvars("%APPDATA%/Roaming/FileZilla/sitemanager.xml")
(Note the leading %) (注意领先的 %)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.