Home > front end >  Can pandas read_csv finish gracefully when no file is found?
Can pandas read_csv finish gracefully when no file is found?

Time:10-05

When I search for pandas's read_csv function when no file is found I find many question asking "why is my file not found if it exists?"

My question is different. I know the file does not exist. So when I call read_csv I get a FileNotFoundError.

I know I can put a try except to catch the error, but is there a graceful way to indicate the function that I don't want an error, just for example return an empty value or none?

CodePudding user response:

The job of read_csv is to read a file, not check if it exists or not. You can do that using pathlib's Path. You can pass a Path object t read_csv instead of a string too, which means you can do this:

from pathlib import Path
import pandas as pd

file=Path("path/to/file.csv")

if file.exists():
    df=pd.read_csv(file)
    ...

You'll have to decide what to do if the path doesn't exist. The simplest option would be to just not proceed with processing.

It's not enough to return an empty Dataframe if the calling code expects it to have specific columns. You'll have to create a dataframe with those columns but no rows.

if file.exists():
    df=pd.read_csv(file)
    ...
    return df
else:
    column_names = ["ColA", "ColB", "ColC"]
    return pd.DataFrame(column_names)

CodePudding user response:

"Exception Handling When an error occurs, or exception as we call it, Python will normally stop and generate an error message.

These exceptions can be handled using the try statement:"

Since you don't want to stop your function in my opinion the only way is to use try except.

In your case this would be something like this :

def function ()
try:
df = pd.read_csv('csv name')
except:
print("An exception occurred")
else: 
df = pd.read_csv('csv name')

CodePudding user response:

You can catch an error to return an empty dataframe with pandas.DataFrame constructor.

try:
    df = pd.read_csv('a_non_exsiting_file.csv')

except FileNotFoundError:
    df = pd.DataFrame()

# Output :

print(df)

Empty DataFrame
Columns: []
Index: []
  • Related