Python Forum
Dataload() crash when filename is not found - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Dataload() crash when filename is not found (/thread-14593.html)



Dataload() crash when filename is not found - Mark3232 - Dec-08-2018

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


RE: Dataload() crash when filename is not found - buran - Dec-08-2018

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