Python Forum

Full Version: Dataload() crash when filename is not found
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Python-forum,

My problem is that whenever an incorrect name "file.txt" is entered into my dataload function, it crashes instead of printing the error.

import pandas as pd
def dataLoad(filename):

    data = pd.read_csv(filename,",")
    try:
        data
    except FileNotFoundError:
        print("Sorry the file wasn't found")
    else:
        print("Data was succesfully loaded")
    return data
When I run it with a incorrect filename

dataLoad("thisfiledeosnotexist.txt")
and this is what I get
Error:
FileNotFoundError: File b'thisfiledeosnotexist.txt' does not exist
Thank you all for taking time reading my post and helping :)

-mark
import pandas as pd
def dataLoad(filename):
    try:
        data = pd.read_csv(filename,",")
    except FileNotFoundError:
        print("Sorry the file wasn't found")
    else:
        print("Data was succesfully loaded")
        return data
Note that when file not found your function will return None