Pandas can't open a csv file FileNotFoundError [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am having a hard time opening a csv with Pandas. I have tried the example in 'Pandas for Everyone' book, have googled many times and last example was here https://analytics4all.org/2016/05/09/python-read-csv-and-excel-with-pandas/
The python program is in a folder called 'lbcsv' then the csv files are in another folder within that one called csv. I have tried not using full path, placing csv file in same folder as the program, then moving them to another folder in the same directory. Just does not seem to want to open the csv. I have tried without the encoding and sep. Before this I had to uninstall pandas and numpy then reinstall because it was giving an error about numpy. I have got around this before but did not ask the question on here so I have no documentation of how I did so.
import pandas as pd
import numpy
servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
print(servers.head())
Traceback (most recent call last):
File "C:/Users/a089673/Desktop/lbcsv/pandaslb.py", line 4, in <module>
servers = pd.read_csv('C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv', encoding='utf-8', sep=',')
FileNotFoundError: File b'C:\\Users\\a089673\\Desktop\\lbcsv\\csv\\server.csv' does not exist

For completeness, \a is an escape character and that is causing issue for you. Using raw string as mentioned in the comments solve this issue.
You can see this clearly if you do repr(file_path) . This is one of the gotchas with Windows. I would suggest using forward slashes for accessing files even in Windows to avoid running into these issues.

Can you try below code
df=pd.read_csv('C:\\\\Users\\\\a089673\\\\Desktop\\\\lbcsv\\\\csv\\\\server.csv')
df
Hope it helps !

Answer was to add the 's' at the end of the file name and to use the \ in the file path.

Related

How can I make Pandas read my csv file proparly because it now puts every value into the first column? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have a question about pandas. I am trying to open the raw_fighter_details.csv file on this kaggle page: https://www.kaggle.com/datasets/rajeevw/ufcdata?select=raw_fighter_details.csv via pandas in Jupyter Notebook.
This is the code I'm using:
import pandas as pd
df = pd.read_csv("C:/Users/jimwe_/OneDrive/Bureaublad/raw_fighter_details.csv")
df.head()
It is not opening correctly. All the seperate columns have been made such as: fighter_name, Height and Weight but it is putting all the data in the rows below into the fighter_name column.
It is however correctly opening another csv file. The one on this kaggle page: https://www.kaggle.com/datasets/harlfoxem/housesalesprediction
Does anyone know why the first file is not opening correctly and if so do you have a solution?
From the first link https://www.kaggle.com/datasets/rajeevw/ufcdata?select=raw_fighter_details.csv
I've downloaded the zip file (4MB), and extracted "raw_fighter_details.csv", but there seems to be no problem

Flask read csv file with pandas - File Not Found Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am trying to load a CSV into a pandas dataframe and I'm getting a "No such file or directory" error.
My project structure is like this:
The function that loads the data from the csv is as such:
def generate_1():
# Read data from CSVs
current_df = pd.read_csv(url_for('static', filename='data/test.csv'))
The error says:
FileNotFoundError: [Errno 2] No such file or directory: '/static/data/test.csv'
And yet we can see the file is on that path in the project directory.
Any ideas what is happening? Thank you.
Its because the application tries to search the CSV file in the root directory of the host machine. To solve it, you might want to consider using relative paths.
./static/data/test.csv # if the file in a static folder inside the execution context
../static/data/test.csv # if the file is in the parent directory
../../static/data/test.csv # if the file is in the grandparent directory
In your case, you'd be interested with:
./static/data/test.csv
or use os.path.relpath:
>>> from os.path import relpath
>>> relpath('./static/data/test.csv')

os python module making other code disfunctional [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am making a program that needs a way to create folders, but the only way I found is the OS module. However, it messes with the code that saves and writes to files. Example:
import os
# necessary code that creates folder
os.chdir("Example_directory/")
os.mkdir("New_folder_name")
# necessary code that writes to a text file
file = open("File_directory/document.txt", "w")
file.write("Text sample")
file.close()
And the error message is:
File "C:\Users\First Name Last Name\Desktop\sample\main.py", line 8, in <module>
file = open("File_directory/document.txt", "w")
FileNotFoundError: [Errno 2] No such file or directory: 'File_directory/document.txt'
Even though the directory does exist.
and to prove the os module is the thing messing with the code, when I removed the os module and its block of code, I didn't receive any errors.
My main point is: Does anybody know an alternative way create folders in python?
File_directory presumably exists in the process's original working directory. But you changed its working directory with os.chdir(), and it doesn't exist in Example_directory.
There's no need to use os.chdir() before os.mkdir(), just create it as a subdirectory.
os.mkdir("Example_directory/New_folder_name")

Python won't load text file without two extensions: FileNotFoundError: [Errno 2] No such file or directory [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Okay so I am very new to Python. Been working on a project, but for whatever reason I get the
"FileNotFoundError: [Errno 2] No such file or directory: 'yt.txt'"
unless in Python I call the extension twice. I'll try to better describe it below.
def mp():
with open("yt.txt", "r") as my_file:
str = my_file.read()
print(str)
mp()
Above is the code. The script won't work unless it's named yt.txt.txt I've tried searching here and other forums but I can't seem to figure it out. The file IS in the same directory as the script, it works with the double extension just not with the single extension. Any help would be appreciated. If you need any additional info just let me know.
I take it you have "show known file extensions" in windows ON so you can confirm the obvious - that its not actually called yt.txt.txt?
[Not too proud to admit stupid stuff like that has tripped me before!]
I'm dumb, thank you to Amiga500 and Barmar for chiming in. The answer was I turned on the "show known file extensions" and it showed the file as yt.txt.txt. I just renamed it, I feel really goofy lol. Thanks all.

Glob returns back an empty list in Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
My code:
hi = glob.glob("hi/FEC[0-9][0-9][0-9][0-9][0-9]_[0-9].seq")
I'm using a glob module and I'm trying to get all the path file names but all it returns back is a empty list. I don't know why it keeps doing that. It worked before my code was accidentally erased.
[]
Regardless if the file isn't even there, it still gives me an empty list.
Update:
So the following files I have are
FEC00001_1.seq
FEC00002_1.seq
FEC00002_2.seq
and so on..
Update 2:
So it I just realized that it might have to do something with the wrong folder like you guys said.
Because right now, my script is in "folder1" and all the files I'm trying to access is in "folder2"
/mainfolder/folder1/script.py
/mainfolder/folder2/files im trying to access
glob.glob is done by using the os.listdir() and fnmatch.fnmatch() functions in concert. So it's important that you search at the right place.
Considering your file hierarchy:-
/mainfolder/folder1/script.py
/mainfolder/folder2/files im trying to access
try the following :-
hi = glob.glob("../folder2/FEC[0-9][0-9][0-9][0-9][0-9]_[0-9].seq")

Categories