-2

I want to be able to pass in a filepath to my python function as a parameter. To be able to make my code reliable , if any other dev was to run my code on their machine , the file path would change based on the name of the user. How can i make this change dynamically within python.

For example my function takes the following arguments :

def f_path(path):

my path will be : /Users/jame/Desktop/data-code/Testdata

i want to be able to change this file path dynamically by changing my name 'jame' to the current user's name .

shell dude
  • 49
  • 1
  • 2
  • 8
  • 1
    https://stackoverflow.com/questions/4028904/how-to-get-the-home-directory-in-python – meowgoesthedog Jan 15 '19 at 10:37
  • So your paths are something like "/Users/USER_1/Desktop/data-code/Testdata", "/Users/USER_2/Desktop/data-code/Testdata", "/Users/USER_3/Desktop/data-code/Testdata"? Meaning that only the username changes and all the rest remains the same? – Employee Jan 15 '19 at 10:41
  • Yes correct @Lossofhumanidentity – shell dude Jan 15 '19 at 10:48
  • This question is really not a duplicate of any question that i have looked through @Georgy – shell dude Jan 15 '19 at 10:49
  • 1
    The "*Desktop/data-code/Testdata*" part that comes after user's home path, doesn't make it less duplicate. – CristiFati Jan 15 '19 at 10:54

5 Answers5

2

Get the current working directory as

import os
current_directory = os.getcwd()
gautamaggarwal
  • 291
  • 2
  • 10
0

If all the desired paths are in the form

  • /Users/USER_1/Desktop/data-code/Testdata

  • /Users/USER_2/Desktop/data-code/Testdata

  • /Users/USER_3/Desktop/data-code/Testdata

We can therefore define the following function that will do the job:

def foo(username):
    first = "/Users/"
    last = "/Desktop/data-code/Testdata"
    result = first + username + last 
    return result  

This function accepts a string username as input parameter and takes care of returning the desired path of interest.

Examples:

Doing foo("Robert") will result in /Users/Robert/Desktop/data-code/Testdata

Doing foo("David") will result in /Users/David/Desktop/data-code/Testdata

Employee
  • 2,434
  • 3
  • 22
  • 42
0

You can use pathlib to get your home folder and construct path with it:

from pathlib import Path

# home would contain something like "/Users/jame"
home = str(Path.home())

path = home + "/Desktop/data-code/Testdata"

Or, if you want to substitute your user's begining of path with the right user's path with something like "translator":

from pathlib import Path
import re

def f_path(path):
    """
    path: str full path, with user's home folder, 
          would be translated to current user's home folder,
          for example, "/Users/jane/some/path" would be translated to
          "/Users/tom/some/path", if current user is Tom.
    """
    # current home folder
    home = str(Path.home())
    # creating regular expression like "^/Users/[^/]+", for later use:
    path_reg = "^" + re.sub("[^/]+$", "", home) + "[^/]+"
    # replacing old home path part to a new one
    return re.sub(path_reg, home, path)

It should work on almost any OS.

MihanEntalpo
  • 1,742
  • 1
  • 12
  • 24
0

Use os module:

user_home_dir = os.path.expanduser('~')
user = os.path.split(user_home_dir)[-1]
os.path.join('/Users', user, 'Desktop/data-code/Testdata')
#/Users\\User_1\\Desktop/data-code/Testdata'
amanb
  • 4,209
  • 2
  • 14
  • 34
0

I have done the following :

def get_json_location(username=os.getlogin()):
    first = "/Users/"
    last = "/Desktop/data-code/Testdata"
    result = first + username + last
    return result
shell dude
  • 49
  • 1
  • 2
  • 8