Narraxus
Narraxus

Reputation: 41

Python Locating the file path

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.

Upvotes: 0

Views: 366

Answers (3)

appusajeev
appusajeev

Reputation: 2309

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

Upvotes: 1

insane
insane

Reputation: 729

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.

Upvotes: 0

Uku Loskit
Uku Loskit

Reputation: 42050

import os    
os.path.expanduser(path)

Source

Upvotes: 3

Related Questions