0

I am trying to access a file from a Box folder as I am working on two different computers. So the file path is pretty much the same except for the username.

I am trying to load a numpy array from a .npy file and I could easily change the path each time, but it would be nice if I could make it universal.

Here is what the line of code looks like on my one computer:

y_pred_walking = np.load('C:/Users/Eric/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy')

And here is what the line of code looks like on the other computer:

y_pred_walking = 'C:/Users/erapp/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy'

The only difference is that the username on one computer is Eric and the other is erapp, but is there a way where I can make the line universal to all computers where all computers will have the Box folder?

Eric
  • 145
  • 1
  • 11

4 Answers4

1

Yes, there is a way, at least for the problem as it is right now solution is pretty simple: to use f-strings

user='Eric'
y_pred_walking =np.load(f'C:/Users/{user}/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy')

or more general

def pred_walking(user):
    return np.load(f'C:/Users/{user}/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy')

so on any machine you just do

y_pred_walking=pred_walking(user)

with defined user before, to receive the result

Igor sharm
  • 291
  • 1
  • 9
1

You could either save the file to a path that doesn't depend on the user: e.g. 'C:/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy'

Or you could do some string formatting. One way would be with an environment or configuration variable that indicates which is the relevant user, and then for your load statement:

import os 
current_user = os.environ.get("USERNAME")  # assuming you're running on the Windows box as the relevant user
# Now load the formatted string. f-strings are better, but this is more obvious since f-strings are still very new to Python 
y_pred_walking = 'C:/Users/{user}/Box/CMU_MBL/Data/Calgary/4_Best_Results/Walking/Knee/bidir_lstm_50_50/predictions/y_pred_test.npy'.format(user=current_user) 
silver
  • 316
  • 1
  • 5
1

Simply search the folders recursivly for your file:

filename = 'y_pred_test.npy'

import os
import random

# creates 1000 directories with a 1% chance of having the file as well
for k in range(20):
    for i in range(10):
        for j in range(5):
            os.makedirs(f"./{k}/{i}/{j}")
            if random.randint(1,100) == 2:
                with open(f"./{k}/{i}/{j}/{filename}","w") as f:
                    f.write(" ")

# search the directories for your file
found_in = []

# this starts searching in your current folder - you can give it your c:\Users\ instead 
for root,dirs,files in os.walk("./"):
    if filename in files:
        found_in.append(os.path.join(root,filename))

print(*found_in,sep = "\n")

File found in:

./17/3/1/y_pred_test.npy
./3/8/1/y_pred_test.npy
./16/3/4/y_pred_test.npy
./16/5/3/y_pred_test.npy
./14/2/3/y_pred_test.npy
./0/5/4/y_pred_test.npy
./11/9/0/y_pred_test.npy
./9/8/1/y_pred_test.npy

If you get read errors because of missing file/directory permissions you can start directly in the users folder:

# Source: https://stackoverflow.com/a/4028943/7505395

from pathlib import Path
home = str(Path.home())   
found_in = []
for root,dirs,files in os.walk(home): 
    if filename in files:
        found_in.append(os.path.join(root,filename))

# use found_in[0] or break as soon as you find first file
Patrick Artner
  • 43,256
  • 8
  • 36
  • 57
1

You can use the expanduser function in the os.path module to modify a path to start from the home directory of a user

https://docs.python.org/3/library/os.path.html#os.path.expanduser