0

I have recently begun working on a new computer. All my python files and my data are in the dropbox folder, so having access to the data is not a problem. However, the "user" name on the file has changed. Thus, none of my os.chdir() operations work. Obviously, I can modify all of my scripts using a find and replace, but that won't help if I try using my old computer.

Currently, all the directories called look something like this: "C:\Users\Old_Username\Dropbox\Path"
and the files I want to access on the new computer look like: "C:\Users\New_Username\Dropbox\Path"

Is there some sort of try/except I can build into my script so it goes through the various path-name options if the first attempt doesn't work?

Thanks!

varsha
  • 1,620
  • 1
  • 16
  • 29
Necarion
  • 105
  • 1
  • 6
  • I really didnt get it, what is your problem actually can you be more specific please? If accessing is not a problem, so what is? – GLHF Dec 18 '14 at 05:55
  • I have dozens of specific directory paths listed in my program. I could theoretically get the code to work on the new machine if I went through and changed every single path. However, this is not practical (as it would break the code on my first machine, for example). I want to know a more general method for changing all the paths in the code. – Necarion Dec 18 '14 at 05:59
  • Well you can use regular expressions for that probably, did you try anything? – GLHF Dec 18 '14 at 06:04

3 Answers3

0

Thought the file sq.py with these codes(your olds):

C:/Users/Old_Username/Dropbox/Path

for x in range:
    #something
def Something():
    #something...

C:/Users/Old_Username/Dropbox/Path

Then a new .py file run these codes:

with open("sq.py","r") as f:
    for x in f.readlines():
        y=x
        if re.findall("C:/Users/Old_Username/Dropbox/Path",x) == ['C:/Users/Old_Username/Dropbox/Path']:
            x="C:/Users/New_Username/Dropbox/Path"
            y=y.replace(y,x)

        print (y)

Output is:

C:/Users/New_Username/Dropbox/Path
for x in range:
    #something

def Something():
    #something...

C:/Users/New_Username/Dropbox/Path

Hope its your solution at least can give you some idea dealing with your problem.

GLHF
  • 3,835
  • 10
  • 38
  • 83
0

Any solution will involve editing your code; so if you are going to edit it anyway - its best to make it generic enough so it works on all platforms.

In the answer to How can I get the Dropbox folder location programmatically in Python? there is a code snippet that you can use if this problem is limited to dropbox.

For a more generic solution, you can use environment variables to figure out the home directory of a user.

On Windows the home directory is location is stored in %UserProfile%, on Linux and OSX it is in $HOME. Luckily Python will take care of all this for you with os.path.expanduser:

import os
home_dir = os.path.expanduser('~')

Using home_dir will ensure that the same path is resolved on all systems.

Community
  • 1
  • 1
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

Knowing that eventually I will move or rename my projects or scripts, I always use this code right at the beginning:

import os, inspect

this_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

this_script = inspect.stack()[0][1]
this_script_name = this_script.split('/')[-1]

If you call your script not with the full but a relative path, then this_script will also not contain a full path. this_dir however will always be the full path to the directory.

Godrebh
  • 354
  • 2
  • 13