How to get a file using %APPDATA% in python 3.9?
Question:
I want to store in a variable this path 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.
Anyone helps?
Answers:
You can retrieve the env variable with getenv
function from os
module. You can also use Path
to easily manipulate paths.
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. On Windows, $ and % forms are accepted.
import os
file = os.path.expandvars("%APPDATA%/Roaming/FileZilla/sitemanager.xml")
(Note the leading %)
I want to store in a variable this path 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.
Anyone helps?
You can retrieve the env variable with getenv
function from os
module. You can also use Path
to easily manipulate paths.
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. On Windows, $ and % forms are accepted.
import os
file = os.path.expandvars("%APPDATA%/Roaming/FileZilla/sitemanager.xml")
(Note the leading %)