FileNotFoundError: [Errno 2]: Reading a file via jupyter - python-3.x

i started using Jupyter and fell into a problem. Whilst trying to read a file called 'vgsales' it gave an error:
FileNotFoundError: [Errno 2] File vgsales.csv does not exist: 'vgsales.csv'
I have put the 'vgsales' folder which has my csv file within it in my desktop with the Jupyter notebook.
Not sure where i went wrong, Please help!
Thanks
Code used:
import pandas as pd
df = pd.read_csv('vgsales.csv')
df

Though the notebook is on your desktop, you still can find out which folder it is in by simply right click on it and choose property. Then you can copy your csv file to the same folder
To list which files are in local folder when you are inside a notebook, use !ls, note that you need ! for the command to work

Related

FileNotFoundError: [Errno 2] No such file or directory for Streamlit

I am trying to make a streamlit dashboard but just to get started, I am trying to import my csv file with data. I added the csv file into the file in which I am working with on vscode which is pictured. I also coppied the path of the csv file which is
"C:\Users\solim\AppData\Roaming\.anaconda\navigator\.anaconda\navigator\scripts\FinalProject\survey.csv"
this path matches up to the path of where my python code is that I run for streamlit but once again it keeps saying that the file cannot be found even though it is exactly where my python code is I just do not understand and it is very frustrating.
Error when I run:
Proof that the file name is spelled correct and the file is in my folder:
I am expecting the file data to be printed on my streamlit page.
screenshot of launch.json code
Enter "args": ["run", "${file}"] into j.son file
Follow the steps from this tutorial to get to launch.json. After, the file should be found

Unable to read downloaded file after converting python file to .exe

I have written some code in python with the help of selenium which is doing web crawling in which it goes to a website after entering login id and password and downloaded a file. I have written the code in jupyter notebook with file name as GSPL_Code.ipynb and with the use of !jupyter nbconvert --to script GSPL_Code.ipynb and !pyinstaller GSPL_Code.py, I have first converted .ipynb file to .py and then made a .exe file from the .py file.
If I run the .exe file which is named as GSPL_Code and it is stored in dist/GSPL_Code then a file crv_report.xlsx is downloaded in the dist/GSPL_Code folder where the executable file is also there but the problem is code does not executed after downloading a file in that location. It got stuck. Nothing is happening now. When I run in jupyter notebook then I don't face any problem but after converting it into .exe, I am facing the problem.
Please find the following python code which is part of the whole script:
def handler(driver):
curr=driver.current_window_handle
for handle in driver.window_handles:
driver.switch_to.window(handle)
if handle != curr:
driver.close()
for handle in driver.window_handles:
driver.switch_to.window(handle)
print(driver.current_url)
click_event('//img[#id="IconImg_crv_report_toptoolbar_export"]')
click_event('//div[#style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
click_event('//span[#title="Microsoft Excel Workbook Data-only"]')
sleep(5)
click_event('//td[#class="wizbutton"]')
sleep(15)
print("Report is downloaded")
PATH1="C:\\Users\\ankit19.gupta\\Desktop\\Test_GSPL\\dist\\GSPL_Code"
crv_report= "/crv_report.xlsx"
print("Reading File")
dataframe1 = pd.read_excel(PATH1+crv_report)
print("Data is stored in the dataframe")
return dataframe1
I have written some print statements to see where the error is coming. So, here I am unable to see the print statement "Report is downloaded". Before this click_event('//td[#class="wizbutton"]') is used to click a button which download the file in chrome browser and I can see this downloaded file in the path dist/GSPL_Code directory and it got executed successfully but after that it got stuck and nothing happened.
I am unable to find what is happening here. Can anyone please help me. Any help would be appreciated.
Edit: There were some delay due to which print statements "Report is not downloaded" and "Reading File" were not showing in console but now I am able to see it but it is unable to read the file crv_report which is on the path PATH1 and it got stuck here and due to which I am unable to see print statement Data is stored in the dataframe
I think code is unable to access the file because os.remove is also not working when I try to delete the crv_report file. Can anyone please help me.
OS Directory Change:
When refactoring python files, in your case changing .py to .exe, it's important to make sure everything is a part of the current working directory. Simple OS checker command:
cwd = os.getcwd()
If directories are misaligned, you will need to run an OS command to change the directory before interacting with any files:
os.chdir('/your/desired/directory')

Can't open and import files on Python

I just started learning Python this month with no prior coding knowledge. Before I could even get to practice coding, I was stuck with opening & importing files! To this end, I have no idea how to fix the problems as a newbie, so I will try to explain as best as I can.
1. Opening files.
Working directory is correct. The csv file was created with Geany on the Ubuntu operating system.
name=[]
age=[]
height=[]
with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
csv file:
reader = csv.reader(csvfile, delimiter=’,’)
for row in reader:
name.append(row[0])
age.append(row[1])
height.append(row[2])
print("Done!")
**Error message:**
File "<ipython-input-3-f801fef0d65e>", line 5
with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
^
SyntaxError: invalid character in identifier
2. Importing files
I downloaded a whatsapp chat text file for a coding exercise. It is stored in my Desktop environment. However, when I attempted to import it on Jupyter Notbook it didn't work.
I believe this is the problem, although I do not know how to work-around this:
the txt file's location as shown in my terminal:
/Users/Mac/Desktop
while the working directory as shown on Jupyter is:
/home/stephanie/Desktop
with open ("_chat.txt") as f:
data = f.readlines()
**error message:**
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-5-07cb5c381c47> in <module>
----> 1 with open ("_chat.txt") as f:
2 data = f.readlines()
FileNotFoundError: [Errno 2] No such file or directory: '_chat.txt'```
Any input or advice is greatly appreciated. Thanks very much in advance!
For the first error, try changing the apostrophes in the open line. For example, change ’/home/stephanie/chapter5exercise.csv’ to '/home/stephanie/chapter5exercise.csv'.
Apparently, the apostrophe (’) that you are using is not the correct one.
For the second error, try giving the entire path of the txt file.

Can't rename a directory that contains thousands of files

The directory I am trying to rename has around 5k parquet files.
Unfortunatelly, os and shutil libraries aren't helping with it
import os, shutil
os.rename('/dbfs/FileStore/AllInOneParquets/SdId=791221', '/dbfs/FileStore/AllInOneParquets/test1')
shutil.move('/dbfs/FileStore/AllInOneParquets/SdId=791221', '/dbfs/FileStore/AllInOneParquets/test2')
Both tries above failed.
os.rename gave me the following exception:
OSError: [Errno 7] Argument list too long
The shutil.move, which tries to use os.rename in the background, started to move the files instead of really renaming.
The 5k files are just a test and I am going for much more than it. Is there a way around?
Try google: python how to move files from one directory to another and you will found the answer :)
How to move a file in Python
and I thing you shod run the code on administrator mode maybe

HDF error when atempting to read netCDF file using netCDF4 module in python

When I try to open a netcdf4 file containing tropomi data of the ESA copernicus mission, I run into the following error.
[Errno -101] NetCDF: HDF error: b'5P_NRTI_L2__CO_____20190505T104819_20190505T105319_08073_01_010301_20190505T124936.nc'
the code that I use is simply
import netCDF4
rootgrp = netCDF4.Dataset(5P_NRTI_L2__CO_____20190505T104819_20190505T105319_08073_01_010301_20190505T124936.nc, "r",format="NETCDF4")
If i re-install netCDF4 or shutdown my computer a few times it succeeds in reading the file again (though this trick does not always work). But it's just a matter of time before it starts failing again. Does anyone have any idea what causes this problem? I work under Ubuntu 18.
python-netCDF4 might be buggy, here is an example with another library which normally works for me (https://github.com/shoyer/h5netcdf):
import h5netcdf.legacyapi as netCDF4
with netCDF4.Dataset('mydata.nc', 'w') as ds:
...

Resources