how to change the path name according to my system

366 views Asked by At

below code is from github and i what to change it according to system path

with open("./output/cifar_inception_plot.pkl", 'rb') as f:
dat = pickle.load(f)
total_inception = dict({})
for item in dat:
    allis = dat[item]
    allis = [x[0] for x in allis]
    total_inception[os.path.basename(item)] = np.array(allis)

when i tried to change it like code below:

with open("./Users/Amulya/Desktop/cifar_inception.pkl", 'rb') as f:
dat = pickle.load(f)
total_inception = dict({})
for item in dat:
    allis = dat[item]
    allis = [x[0] for x in allis]
    total_inception[os.path.basename(item)] = np.array(allis)

i got error

--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) in 77 } 78 ---> 79 with open("./Users/Amulya/Desktop/cifar_inception.pkl", 'rb') as f: 80 dat = pickle.load(f) 81 total_inception = dict({}) FileNotFoundError: [Errno 2] No such file or directory: './Users/Amulya/Desktop/cifar_inception.pkl'

iam still getting error any solution on how to write the filename corretly

3

There are 3 answers

1
Greg Roughton On

If file is in the same directory as the python script you're running you can just use the name of the file itself open("cifar_inception.pkl") otherwise you can use various utils in the os library. Ultimately you need to know where the file is on your system and what the full path is, when in doubt use the full path from root assuming you're on Mac OS based on the path you provided it it might just be "/Users/Amulya/Desktop/cifar_inception.pkl".

1
IamRichter On

./Users/Amulya/Desktop/cifar_inception.pkl does not exist.

The ./ means "in this directory". For example: I am in my home folder: /home/me/ And I want to access my Download folder. In stead of type "/home/me/Download" I could just use the "./Download".

Your program is trying to look for a folder named User inside it's current path.

Just remove the single dot from the absolute path, and it will probably work.

1
José Pedro Silveira Martinez On

The symbol . indicates you are providing a relative path, which is relative to your current working directory. I suggest you to change the string representing the path of the file you are trying to open to a string containing the absolute path of the file.

If you can access the file via a file manager app with GUI (e.g. Windows Explorer on Windows, Nautilus on Ubuntu), you can easily check the absolute path of the file by checking its properties. In case you can access the file's directory via command-line, PWD is an environment variable that outputs the current directory's absolute path. You can check it with echo $PWD and append the filename (cifar_inception.pkl) to the command's output in order to obtain the file's absolute path.