0

I'd like to define a constant in my script like that path to my Dropbox folder. Most my scripts will try to load some data of Dropbox which is shared among my PCs, but I find that between Mac and Ubuntu the prefix is different (/Users/<user>/Dropbox versus /home/<user>/Dropbox).

Is there a way to save this kind of information in some variable that will be loaded in each session such that I could have a global variable like DROPBOX (what would be a good convention, __DROPBOX__?) as path prefix to a file name, e.g. fname = DROPBOX + "myfile.txt".

Kind of reminds of me defining this in one's .Rprofile which holds settings in R.

Or is there a better way to handle this?

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160

2 Answers2

2

You could use the built in environment variables to get the path to the user home directory:

import os
print os.environ['HOME']

Which would solve your problem is a way that is more likely to remain stable if run on a new machine.

Tom Anthony
  • 791
  • 7
  • 14
0

How about this:

os.path.expanduser('~/Dropbox')

or you can just try different alternatives:

dirs_to_try = ('/Users/Guido/Dropbox', '/home/Guido/Dropbox')
for path in dirs_to_try:
    if os.path.isdir(path):
        break
finally:
    print 'cannot find Dropbox directory'
    path = None
m.wasowski
  • 6,329
  • 1
  • 23
  • 30
  • I mean... one way would be to make it a function globally, but then still I would basically want it to just be available in ANY python session, from the get-go. That's the problem in the first place. – PascalVKooten Oct 02 '14 at 07:38