0

I made a simple code that loads in data called 'original.dat' from a folder named 'data' in my computer. The code was working great and i was able to view my spectra graph. This morning I ran the code again, but it completely crashed giving the error " OSError: data/original.dat not found." Even though nothing changed. The data is infact still in the folder named 'data' and there isn't any spelling mistakes. Can anyone please help understand why its now giving me this error? The code was working perfectly the day before. here is the code I used :

    %matplotlib inline
    import numpy as np
    import matplotlib.pyplot as plt

    OPUSimaginary = np.loadtxt('data/original.dat', delimiter = ',')

Data file position, Error:cant find the file,Error: suggested code to find file

4
  • 1
    Can you try to execute import os print(os.path.isfile(fname)). If the file is found the it should return True Commented Oct 25, 2020 at 14:21
  • gives there following: --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-a2f2dd07f84a> in <module>() 1 import os ----> 2 os.path.isfile(Original.dat) NameError: name 'Original' is not defined Commented Oct 25, 2020 at 15:17
  • please see image above for what happens when i use your suggestion, thank you Commented Oct 25, 2020 at 15:22
  • Enclose the filename in quote. import os print(os.path.isfile('Original.dat')) Commented Oct 27, 2020 at 2:55

1 Answer 1

1

Few things that you can do to avoid file not found type of issues:

  1. Add a check if the file exists before trying to load it. Example:

    import os
    os.path.isfile(fname) # Returns True is file found
    
  2. Make sure the file permissions are correct and the file can be read.

    sudo chmod -R a+rwx <file-name>
    
Sign up to request clarification or add additional context in comments.

5 Comments

the first suggestiong gives the following error: --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-a2f2dd07f84a> in <module>() 1 import os ----> 2 os.path.isfile(Original.dat) NameError: name 'Original' is not defined
please see image above for what happens when i use your suggestion, thank you
Enclose the filename in quote. import os print(os.path.isfile('Original.dat'))
Hwllo, thank you very much for your reply and guidance. the os package returned 'False' which if i understand correctly means the file was not found. Should be saving the data in a different way in my computer in order to access the file? i.e on desktop, dowloads etc) Can you suggest a more efficient way? I am little confused on the directory of how to obtain the file in pythob now since the code was working previously. Thank you in advance
@jojo In the terminal, please check the absolute path of the file and use the same path. If the os.path.isFile is returning False that means either the path to file is wrong or the file is not found. In your case, I think it is due to path as you are sure that the file is present. In terms of best practices, it is always good to check if a file is found before performing any operation on it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.