Python Forum
Dataload() crash when filename is not found
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
Reply
#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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 5 3,696 Nov-23-2020, 05:15 PM
Last Post: cananb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020