How can I open a file from the place where everything is, but under a different name?
I am trying to open a .txt file in the appdata folder, but I cannot figure out how to make python open from a specified directory with different names
import os
f=open("os.getenv('APPDATA')/Local/filedir/filename.txt",'r+')
which I came up with after a few minutes of googling, but I really don't know how I should implement os.getenv ('APPDATA') to work. if I print it out, it says the right path, but if I put it along with the rest of the path, it says it can't find it.
source to share
With your code, you are telling python to open the file with the path os.getenv(...
, while you should be using the return value os.getenv
.
According to this , Windows has its own environment variable for the directory Local
, namely LOCALAPPDATA
. This should work (I'm not on Windows, so I can't test it):
import os
subpath ="filedir/filename.txt"
print os.path.join(os.getenv("LOCALAPPDATA"), subpath)
source to share