I'm using Jupyter/ ipython to attempt to load a .csv file with the open() function on Windows. First, I type the command 'pwd' to display the current working directory, and the following shows up:

'd:\\my data\\documents\\notebooks'

I try using the following code to try to load the file, which does not work:

data_file = open("D:\\my data\\documents\\notebooks\\MNIST\\mnist_train_10.csv", 'r')
data_list = data_file.readlines()
data_file.close()

I have also tried the following variations of this, removing the entire filepath and only having the local folder path within the current directory:

data_file = open("\\MNIST\\mnist_train_10.csv", 'r')
data_list = data_file.readlines()
data_file.close()

and also, I've experimented with removing the double backslashes and also tried with forward slashes, with no success. I read online that Windows can be funny about forward versus backslashes in python.

data_file = open("/MNIST/mnist_train_10.csv", 'r')
data_list = data_file.readlines()
data_file.close()

this is the error that I get:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-25-65ea2d6f0c09> in <module>()
----> 1 data_file = open("D:\\my data\\documents\\notebooks\\MNIST\\mnist_train_10.csv", 'r')
      2 data_list = data_file.readlines()
      3 data_file.close()

FileNotFoundError: [Errno 2] No such file or directory: 'D:\\my data\\documents\\notebooks\\MNIST\\mnist_train_10.csv'

Does it make a difference if the file is in the d: drive rather than the c: drive? What am I doing wrong here?

fyi this project is part of the "Make your own Neural network" book by Tariq Rashid, but the book doesn't get into the specifics of this.

Can anyone point me to a resource where I can learn more?

Thank you for the help - I am just starting to learn, and do not have much experience working with real files and directories.

1 Answers

0
kanishkegb On

The best way to define a path is to use os.path.join, which removes the OS dependency of your code. For your case, below should work:

import os

path = os.path.join("D:", "my data", "documents", \
    "notebooks", "MNIST", "mnist_train_10.csv")

if os.path.exists(path):
    data_file = open(path, 'r')
else:
    print('{} does not exist'.format(path))