“CSV file does not exist” for a filename in jupyter notebook - python

i have the following code.and i have the csv file located in the path but when i run the code it just says that the file dosent exist at all.i moved the code to diff locations and changed the path to the new one and i also changed the name of the file but it didnt worked.also i add ".csv" to the name of the file but no good came of it.
here is the code:
import pandas as pd
import numpy as np
import csv
filename = ("C:\\Users\\Z\\pima-indians-diabetes.data.csv")
df1 = pd.read_csv(filename)
print(df1.head())'''
The error says
FileNotFoundError: [Errno 2] File C:\Users\Z\pima-indians-diabetes.data.csv does not exist: 'C:\\Users\\Z\\diabetes'

You need to give the *.csv file extension in your code. Replace
filename = ("C:\\Users\\Z\\diabetes")
with
filename = "C:\\Users\\Z\\diabetes.csv"

Few steps to check whether the file indeed exists:
Open a Command Prompt (Click Start and type cmd)
Type this command dir C:\Users\Z\pima-indians-diabetes.*
This will clearly show if the file in question really exists.
It might not be there at all.
Or it might be named C:\Users\Z\diabetes.csv.csv.
Latter can happen when saving a file with Save As and typing my-file.csv for the file name, unaware that the application being used also adds the extension .csv, so now there are two of those.
Here is the expected command output for an existing file with correct name:
C:\Users\Z>dir C:\Users\Z\pima-indians-diabetes.*
983 pima-indians-diabetes.data.csv
1 File(s) 983 bytes
0 Dir(s) 220,040,663,040 bytes free

remove '.' from your filename. I think it is effecting your file format

Related

FileNotFoundError: [Errno 2] No such file or directory Pandas

I am trying to read my excel file with pd.read_excel in python.
But ı am get this error message = FileNotFoundError: [Errno 2] No such file or directory
I put my excel file same place with my python file.
pic1 pic2
You can use full path this way to read the excel file. And also add r prefix before the file path so backslash will be treated as literal character
pd.read_excel(r"C:\Users\selman\PycharmProjects\selman_learning\bisiklet_fiyatlari.xlsx")
import pandas as pd
path = 'absolute_path/records.xlsx' #eg. C:\\Projects\\readexcel\\file\\records.xlsx
df = pd.read_excel(path)
Do the above
I think you need to put file object and not only the path of the file.
Try to use:
with open("<path to your file>", encoding = 'utf-8') as f:
pandas.read_excel(f)
I think you can modify the code by writing it in this way:
pd.read_excel("./<file name>")
Providing the absolute path to the .xlsx file worked for me. For situations where you cannot anticipate what the absolute path will be, try the following:
import os.path
pd.read_excel(io=os.path.abspath('path\\to\\excel_file.xlsx'))
'path\to\excel_file.xlsx' should be the relative path to the .xlsx from the project root.
Credit to this answer to a similar question.

How to fix "No such file or directory" error with csv creation in Python

I'm trying to make a new .csv file, but I'm getting a "No such file or directory" in the with open(...) portion of the code.
I modified the with open(...) portion of the code to exclude a direction, substituting a string name, and it worked just fine. The document was created with all my PyCharm scratches on the C Drive.
I believe it's worth noting that I'm running python on my C: Drive while the directory giving me issues exists on the D: Drive. Not sure if that actually makes a difference, but i
path = r"D:\Folder_Location\\"
plpath = pathlib.PurePath(path)
files = []
csv_filename = r"D:\Folder_Location\\"+str(plpath.name)+".csv"
#Create New CSV
with open(csv_filename, mode='w',newline='') as c:
writer = csv.writer(c)
writer.writerow(['Date','Name'])
I expected the code to create a new .csv file that would then be used by the rest of the script in the specific folder location, but instead I got the following error:
File "C:/Users/USER/.PyCharm2018.2/config/scratches/file.py", line 14, in <module>
with open(csv_filename, mode='w',newline='') as c:
FileNotFoundError: [Errno 2] No such file or directory: '[INTENDED FILE NAME]'
Process finished with exit code 1
The error code correctly builds the file name, but then says that it can't find the location, leading me to believe, again, that it's not the code itself but an issue with the separate drives (speculating). Also, line 14 is where the with open(...) starts.
EDIT: I tested a theory, and moved the folder to the C: drive, updated the path with just a copy and paste from the new location (still using the \ at the end of the file path in Python), and it worked. The new .csv file is now there. So why would the Drive make a difference? Permission issue for Python?
The raw string can not end with one single backslash '\' so what you are using in your code like in path = r"D:\Folder_Location\\" is the right thing but actually you don't need any backslashes at the end of your path:
i ran some similar tests like yours and all goes well, only got the same error when i used a non existing directory
this is what i got:
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\python\\myProgects\\abc\\\\sample3.txt'
so my bet is you have a non existing path assigned in path = r"D:\Folder_Location\\" or your path is referring to a file not a folder
to make sure just run this:
import os
path = r"D:\Folder_Location\\"
print(os.path.isdir(path)) # print true if folder already exists
better approach:
file_name = str(plpath.name)+".csv"
path = r"D:\Folder_Location"
csv_filename = os.path.join(path, file_name)

python: zipfile.ZipFile No such file or directory

There is folder path:
P:\\2018\\Archive\\
There are many zipfiles I want to create programmatically, but am starting with test. I will name this test zip file "CO_007_II.zip" and will attempt to create in above location:
import zipfile as zp
with zp.ZipFile("P:\\2018\\Archive\\CO_007_II.zip",'w') as myzip:
myzip.write(r"P:\2018\CO_007_II")
But I get error!
...
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python27\ArcGIS10.2\lib\zipfile.py", line 752, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'P:\\2018\\Archive\\CO_007_II.zip'
Is this not method for creating new zipfile? I know file does not exist. Is why I am using 'w' mode, no?
This is documentation:
https://docs.python.org/3/library/zipfile.html
It says:
'w' to truncate and write a new file
Example on documentation page:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
code worked two days ago to create new zip file but did not add folder. Today nothing works! Why not? All paths valid. How do I create new zip file with python and add folders to it?
I also encountered a similar issue and came here looking for answers. Since this was the top hit, I'll add what I discovered.
The answer provided by #metatoaster didn't work for me, when stepping through the code I found that the path returned true to isdir.
In my case, the path length exceeded the Windows max path length (260 chars) which was causing it to fail despite the folder path being valid.
Hope that helps someone else down the line!
The only way this could be reproduced was to create a zipfile in a directory that does NOT exist yet. The only way to be sure (you cannot trust a file manager; only way to verify is to check from within the program itself) is to assign the desired path of the new zip file to a variable (e.g. path), and then call isdir(dirname(path)). For example:
from os.path import isdir
from os.path import dirname
target = "P:\\2018\\Archive\\CO_007_II.zip"
if not isdir(dirname(target)):
print('cannot create zipfile because target does not exists')
else:
# create the zipfile
I had the same issue. It was the long path. I solved by adding this //?/C at the beginning of the path
path = r"//?/C:\Users\Camilo\Proyectos"

Permission denied when pandas dataframe to tempfile csv

I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:
[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'
import tempfile
import pandas
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name)
Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:
tempfile.tempdir='D:/Username/Temp/'
This gives me the same error message
Edit:
The tempfile appears to be locked for editing as when I change the loop to:
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name + '.csv')
I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.
However, if I change the code to:
with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
training_data.to_csv(temp.name)
I get the same error message as before. The file is not open anywhere else.
I encountered the same error message and the issue was resolved after adding "/df.csv" to file_path.
df.to_csv('C:/Users/../df.csv', index = False)
Check your permissions and, according to this post, you can run your program as an administrator by right click and run as administrator.
We can use the to_csv command to do export a DataFrame in CSV format. Note that the code below will by default save the data into the current working directory. We can save it to a different folder by adding the foldername and a slash to the file
verticalStack.to_csv('foldername/out.csv').
Check out your working directory to make sure the CSV wrote out properly, and that you can open it! If you want, try to bring it back into python to make sure it imports properly.
newOutput = pd.read_csv('out.csv', keep_default_na=False, na_values=[""])
ref
Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.
With the use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp() usage can be replaced easily with NamedTemporaryFile(), passing it the delete=False paramete.
Read more.
After export to CSV you can close your file with temp.close().
with tempfile.NamedTemporaryFile(delete=False) as temp:
df.to_csv(temp.name + '.csv')
temp.close()
Sometimes,you need check the file path that if you have right permission to read and write file. Especially when you use relative path.
xxx.to_csv('%s/file.csv'%(file_path), index = False)
Sometimes, it gives that error simply because there is another file with the same name and it has no permission to delete the earlier file and replace it with the new file.
So either name the file differently while saving it,
or
If you are working on Jupyter Notebook or a other similar environment, delete the file after executing the cell that reads it into memory. So that when you execute the cell which writes it to the machine, there is no other file that exists with that name.
I encountered the same error. I simply had not yet saved my entire python file. Once I saved my python file in VS code as "insertyourfilenamehere".py to documents(which is in my path), I ran my code again and I was able to save my data frame as a csv file.
As per my knowledge, this error pops up when one attempt to save the file that have been saved already and currently open in the background.
You may try closing those files first and then rerun the code.
Just give a valid path and a file name
e.g:
final_df.to_csv('D:\Study\Data Science\data sets\MNIST\sample.csv')

Python: No directory/ file when using xlrd

I have simple code:
from xlrd import open_workbook
open_workbook('Book1.xls')
No matter how I save the file (xls or xlsx) or how I change the path to the file (C:\Users\... or C:\Users...) I keep getting:
IOError: [Errno 2] No such file or directory: '(insert whatever is in place of Book1.xls)'
I don't know what I could be doing wrong. I'm not trying anything specific yet I just want it to not throw up an error.
You are not including whole path of the file. The path will be looks like:
file="C:\\USER\\TEST\\FILENAME"
or
file=r":\USER\TEST\FILENAME"
If you are using single slash then you need to use 'r' or else it will throw error.
you are not doing workbook("Book1.xls")
you are passing it a path (with directories) ... one of the folders in the path doesnt exist
this is probably because you do something like
directory="C:\Users\Blah\..."
when you need to do
directory = r"C:\Users\Blah\..."
other wise the backslashes serve to escape the next character

Categories

Resources