Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Dataload() crash when filename is not found
#1
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
Quote
#2
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
Mark3232 likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trying to teach myself how to code and I'm stuck on a question I found online... abushaa4 1 239 Dec-16-2018, 01:52 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)