I am using Jupyter notebook to import some data from a text file.
The folder from which I have imported the notebook has another file, data.txt but when I try to use the loadtxt() module, the following error appears:
IOError Traceback (most recent call last)
<ipython-input-4-a129a96139d0> in <module>()
----> 1 our_data = loadtxt("data.txt")
IOError: data.txt not found.
I looked for a solution and the manual in the notebook stated that the file may not be in the same directory or folder as your notebook.
I checked twice and found that the folder on my computer contains both the notebook and the data.txt file in the same location.
What is the issue?
The file is simply not in the folder of the output of this code
import os
print(os.getcwd())
You need to either put the data.txt file in this folder or load the file with a path the points to the file.
As far as I know, loadtxt() method is from numpy, so you should addimport numpy as np and use it as np.loadtxt().
Hope this helps!
Can you try using a full path instead of just data.txt?
Maybe the current directory for jupyter is not where the notebook is.
Or you could try printing the current directory, or current directory contents like this to be sure:
import os;print(os.listdir("."))
Related
In PyCharm, I created folder named test, created txt file named test inside and marked folder as content root. When I added python file outside the test folder and tried to access test.txt file with open it suggested me file name. But when I tried running the code it couldn't find the file.
Traceback (most recent call last):
File "/home/elmo/PycharmProjects/TBC_PAY_API_TESTING/testing.py", line 32, in <module>
print(open("test.txt").read())
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
This is how the code and folders look like ( ingore the trash folder)
How can I fix it? The main reason I am doing this is to avoid writing full path's for accessing file.
You are talking about two totally different things in your question. If you mark a folder as Sources root that means the Python interpreter will be able to find the modules in that folder.
For example:
When you write an own module and you want to use it in another file the Python won't find it automatically. The PYTHONPATH should contain the path of the folder which contains your module. And actually the Sources root option does this!
The other thing what you have mentioned in your question is that you don't provided a correct path in your code. It is a real error. In your code, you have to provide the correct path for open. The Pycharm is an IDE but your (or other's) Python interpreter will use your code.
You can solve your problem in many ways.
For example:
You can hard-code the path of your txt (It is totally not recommended):
print(open("/home/elmo/PycharmProjects/TBC_PAY_API_TESTING/test/text.txt").read())
You can use relative path:
print(open("test/text.txt").read())
You can use full path based on your Python file (I recommend this solution):
import os
dir = os.path.realpath(os.path.dirname(__file__)) # Directory of your Python file
file_path = os.path.join(dir, "test", "test.txt") # Create the path of the file
print(open(file_path).read())
I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)
I have uploaded a package to pypi and github. I have then installed the package and tried to use it. It contains a python script which need to read from a file. I have placed both in the same directory.
pip install pycricket
from pycricket import cricket
c = cricket.Cricket()
c.query()
Query() function involves reading from a file. When I see the 'pycricket' package in library, both script as well as file are in same folder.
query():
with open('matches.csv', 'r') as f:
#code
I don't know why I get the error.
You can inspect the current working directory with:
>>> import os
>>> os.getcwd()
If your data is in a different directory (unclear from the question, but likely given the error message), then change to the directory where the data is stores:
>>> os.chdir(path_to_data_directory)
I'm trying to learn the ropes of starting a Python script from Excel VBA using xlwings 0.9.2. According to the docs, I need to change the PYTHONPATH to the path for my py file. I've seen several versions of this question, and various answers, but none have addressed my specific scenario. I have a basic test module "module1.py" and an Excel file "Book2.xlsm" located on the desktop which has a button to run this macro:
Sub macro1()
RunPython ("import module1.py; module1.run_all()")
End Sub
module1 goes like this:
import xlwings as xw
def run_all():
wb.Book.caller()
xw.sheets("Sheet1").range("A1").value = "Done!"
return
I then imported the "xlwings.bas" file and edited the VBA code to read PYTHONPATH = "C:\Users\bwamp\Desktop\module1\module1, which refers to the subfolder that holds "module1.py" (full path: "C:\Users\bwamp\Desktop\module1\module1\module1.py", for clarity). Press the button to run macro1 and I get the following error:
Error
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named 'module1.py'; 'module1' is not a package
Press Ctrl+C to copy this message to the clipboard.
OK
Any ideas what I'm doing wrong?
You have two errors: You need to import your module without the .py ending:
Sub macro1()
RunPython ("import module1; module1.run_all()")
End Sub
And it's xw.Book.caller() instead of wb.Book.caller().
I had the same error. Solved by downgrade to xlwings version 0.7.2
I am using ipython to compile my code, but I am faced with an error.
My code is the following:
import pandas as pd
h5=pd.HDFStore('./source/vstoxx_data_31032014.h5','r')
futures_data=h5['futures_data']
options_data=h5['options_data']
h5.close()
And the OS error is:
OS error ./source/vstoxx_data_31032014.h5`` does not exist
How do I overcome this problem?
You can check present directory using:
%pwd
If the output of the above is any different from where you have save the downloaded file, then you can copy the downloaded file to this location. That way, the command will start reading the file.
Alternatively if copy paste of the desired file is not feasible, then directory can also be changed using the following function:
os.chdir(path)