How to tell python for example, I made a program, which opens a specific .scv
In example csv=open('c:\Users\Luka\Desktop\prvi.csv','r')
But when I make an .exe file, that is required to run on all PCs. How can I swap that "Luka" so it works for any PC that has a prvi.csv file on desktop.
3 Answers 3
import os
os.path.expanduser(path)
1 Comment
"~/Desktop/prvi.csv" for the path to make this answer somewhat more complete.import os
to get username, use
os.environ['username']to construct the path to the file, in case file is on desktop on every machine, use
csv=open(os.environ['homedrive'] + os.environ['homepath'] + '\\Desktop\\prvi.csv','r')
2 Comments
os.path.expanduser("~/Desktop/prvi.csv"), it also works on Mac OS and on many Linux systems.check out
http://docs.python.org/library/getpass.html
Should be
>>> import getpass
>>> getpass.getuser()
Store the username in a variable and substitute in your path name.