0

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.

asked Jun 28, 2012 at 17:45

3 Answers 3

3
import os 
os.path.expanduser(path)

Source

answered Jun 28, 2012 at 17:48
Sign up to request clarification or add additional context in comments.

1 Comment

You should substitute "~/Desktop/prvi.csv" for the path to make this answer somewhat more complete.
1
import os 
  1. to get username, use os.environ['username']

  2. 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')

answered Jun 28, 2012 at 18:22

2 Comments

@LukaMihaldinec: The solution by Uku Loskit is not only much simpler: os.path.expanduser("~/Desktop/prvi.csv"), it also works on Mac OS and on many Linux systems.
I didn't know ("~/Desktop/prvi.csv") this part :P
0

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.

answered Jun 28, 2012 at 17:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.