Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: File Handling with finally | Advanced Exception Handling
Python Error Handling

bookChallenge: File Handling with finally

When working with files in Python, you must ensure that every file you open is also closed properly. This prevents resource leaks and keeps your programs running smoothly. The finally clause is a powerful tool for this purpose: it guarantees that specific cleanup code will run, even if an error occurs while processing the file.

Suppose you try to read from a file that might not exist or could cause an error while being read. No matter what happens, you need to close the file to avoid leaving it open accidentally. By placing the file-closing code inside a finally block, you make sure that the file is always closed, whether or not an exception is raised.

In this challenge, you will practice using finally to handle files safely and reliably.

Tehtävä

Swipe to start coding

Write a function named read_file_contents that takes a single argument, filename (a string). Your function should:

  • Try to open the file and read its contents inside a try block;
  • If an exception occurs while reading the file, print "An error occurred while reading the file.";
  • Always close the file using a finally block, regardless of whether an exception occurred;
  • Return the contents of the file if successful, otherwise return None.

Test your function with both a filename that exists and one that does not exist.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

Awesome!

Completion rate improved to 6.67

bookChallenge: File Handling with finally

Pyyhkäise näyttääksesi valikon

When working with files in Python, you must ensure that every file you open is also closed properly. This prevents resource leaks and keeps your programs running smoothly. The finally clause is a powerful tool for this purpose: it guarantees that specific cleanup code will run, even if an error occurs while processing the file.

Suppose you try to read from a file that might not exist or could cause an error while being read. No matter what happens, you need to close the file to avoid leaving it open accidentally. By placing the file-closing code inside a finally block, you make sure that the file is always closed, whether or not an exception is raised.

In this challenge, you will practice using finally to handle files safely and reliably.

Tehtävä

Swipe to start coding

Write a function named read_file_contents that takes a single argument, filename (a string). Your function should:

  • Try to open the file and read its contents inside a try block;
  • If an exception occurs while reading the file, print "An error occurred while reading the file.";
  • Always close the file using a finally block, regardless of whether an exception occurred;
  • Return the contents of the file if successful, otherwise return None.

Test your function with both a filename that exists and one that does not exist.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 3
single

single

some-alt