Load .mat file in python - python

I am working with on a course by coursera, and wanted to try the scripts locally.
I got this error:
IOError: [Errno 2] No such file or directory: 'datasets/data.mat'
The file is in the directory and I can see it from Finder on MAC, however, it's not visible form the file explorer in Spyder.
I found this question: Read .mat files in Python
and checked that original module according to what is suggested in the first answer, but still the file cannot be reached.
I don't know if it's worth mentioning, but the file has a shortcut icon (even though I've downloaded it directly from coursera).
Why the file is not reachable? and how reach it?

Related

Error creating an executable python script with PyInstaller due to PyBluez error

I have created a functional python (Python 3.10.4) script that uses Pybluez (version 0.30) to connect to an ESP32. Now I want to use PyInstaller to create a standalone executable application from said script, but I keep running into the following error when executing the command:
pyinstaller scriptname.py
Error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\X\Python3\lib\site-packages\pybluez-0.30-py3.10-win-amd64.egg\bluetooth\widcomm.py'
In the Python3\lib\site-packages directory, there is a pybluez-0.30-py3.10-win-amd64.egg file, but it contains no sub folders (bluetooth\widcomm.py).
Any help regarding this error would be much appreciated. Let me know if any additional information is required.
Thank You
Try to reinstall Pybluez and use a stable version, e.g. pybluez==0.23, or add widcomm.py manually https://github.com/pybluez/pybluez/blob/master/bluetooth/widcomm.py.
I managed to solve the mentioned problem, and here is how.
An .egg file is basically like a zip file that contains sub folders and files. I had to download a software called ALZip (https://alzip.en.softonic.com/) to view the contents of the .egg file, which showed that the bluetooth/widcomm.py was in fact present in the .egg file.
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\X\Python3\lib\site-packages\pybluez-0.30-py3.10-win-amd64.egg\bluetooth\widcomm.py'
had an issue with the name of the pybluez-0.30-py3.10-win-amd64.egg file so I solved it by Extracting the .egg file to a new folder, and renaming the folder to "pybluez-0.30-py3.10-win-amd64.egg" (yes the .egg should be part of the name as well)
These steps managed to solve the error and I was able to create an exe from my Python Script using pyinstaller. (pyinstaller scriptname.py)

Can't find and read .csv file

I recently brought my code from Jupyter into VSCode. I fixed up some problems with my imports, but now I can't find a .csv file. The code and .csv file are saved in the same folder. My code is:
import pandas
df = pandas.read_csv('emailss.csv', names=['Email', 'Amiability'])
After running the program, it says,
FileNotFoundError: [Errno 2] No such file or directory: 'emailss.csv'
What's going on here? How can I fix it? It was working perfectly in Jupyter.
Here is a screenshot:
where are you running the code from? Is the CSV file in the same directory as your python file, if yes is the console path the same as the python script and the CSV file?
Check with os.getcwd() that your current working directory matches with the file path

FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt'

The file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program. unless I am misunderstanding what the error means, I just can't figure out what i'm missing or did wrong. i'm just reading a basics python book and this I pretty much copied it 1 for 1.
CODE
with open('test_text.txt') as test_text:
reader = test_text.read()
print (reader)
FileNotFoundError means you are trying to open a file that does not exist in the specified directory (in this case, whatever directory you are running your Python script from).
the file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program
In that case, you need to make sure that you're in the same directory as your file on your command-line, or specify the full path to test_text.txt (e.g. /home/user/Desktop/test_text.txt)
FileNotFoundError occurs when you are trying to access files outside the scope of the application. In this case, the scope of your application is the folder in which you main python file resides.
There are couple of ways through which you can resolve this:
1. Provide an absolute or full path to the file: As mentioned by #pigeonburger in his answer, you can provide a full path. If you are using windows, then full path will be from C:/Desktop/
2. Using a Relative Path: Relative paths is the solution if both the file you want to access and the python program are in the same folder. Relative Paths start with ./ So, based on your file structure, the possible solution is this:
with open('./test_text.txt'):
# your code
Thank you Pointman for your answer. It did not work for me but it did lead me to what worked. I am using Visual Studio Code and I was having this issue with opening a text from a Udemy course.
jabber = open('Jabberwocky.txt', 'r')
for line in jabber:
print(line)
jabber.close()
After running this code I would get the following error message
FileNotFoundError: [Errno 2] No such file or directory: './Jabberwocky.txt'
I tried both the absolute path and the relative path option but they did not work for me. What I found that worked was once the terminal is open using Python you will have to traverse through your files until you get to the said folder or file. Once you do this then run the code and it will work.

Using PYC file instead of a PY

A rookie question here:
I have a PY code and compiled it to create a .pyc. I would want to use this pyc file instead of PY.
I am running the PY file using external program. When the PY exists in the folder everything works perfect. However when I remove the PY file and simply use the pyc I get error:
IOError: [Errno 2] No such file or directory: 'E:/data/test/tech.py'
Whereas I have a tech.pyc lying around in the same folder.Any ideas what could be the issue here?
Normally, python is not compiled. The .pyc files are only a performance optimization that improve loading times.
Python is looking for the .py file because it always checks that first. If a .pyc file is newer than its corresponding .py file, it will then use the .pyc file. If the .py file is newer it will create a new .pyc file.

Error while trying to open CSV file in python

Python rookie here. Trying to use the CSV module, but I get an error:
IOError: [Errno 2] No such file or directory: 'books.csv'
The actual CSV file is stored on my desktop. I suppose this means Python can't find the file? If so, how can I fix this?
Many thanks in advance!
Since your file name includes no path component, it is implicitly assigned to be in the current directory. Unless your current directory is the desktop, you won't be able to find the file.
Either give it a full pathname, or move to the desktop and run your script from there.

Categories

Resources