Python: Check if file exists
File Existence Checker
Write a Python program to check whether a file exists.
Sample Solution-1:
Python Code:
# Import the os.path module to work with file and directory paths.
import os.path
# Check if 'main.txt' is a file and print the result.
print(os.path.isfile('main.txt'))
# Check if 'main.py' is a file and print the result.
print(os.path.isfile('main.py'))
Sample Output:
False True
Explanation:
The said Python code imports the os.path module, which contains functions for interacting with the file system.
- os.path.isfile('main.txt') checks whether a file named 'main.txt' exists in the current directory and returns True if it does and False if it does not.
- os.path.isfile('main.py') checks whether a file named 'main.py' exists in the current directory and returns True if it does and False if it does not.
Then it prints the output of these two function calls.
Sample Solution-2:
Python Code:
# Import the os.path module to work with file and directory paths.
import os.path
# Check if 'main.txt' exists (can be a file or directory) and print the result.
print(os.path.exists('main.txt'))
# Check if 'main.py' exists (can be a file or directory) and print the result.
print(os.path.exists('main.py'))
Sample Output:
False True
Explanation:
The said Python code imports the os.path module, which contains functions for interacting with the file system.
- os.path.exists('main.txt') checks whether a file or directory named 'main.txt' exists in the current directory and returns True if it does and False if it does not.
- os.path.exists('main.py') checks whether a file or directory named 'main.py' exists in the current directory and returns True if it does and False if it does not.
Then it prints the output of these two function calls.
The difference with previous example is that, os.path.exists() checks for the existence of file or directory, where as os.path.isfile() is used to check for the existence of file only.
Sample Solution-3:
Python Code:
# Open the file 'main.py' for reading.
my_file = open('main.py')
try:
# Close the file.
my_file.close()
# Print "File found!" if the file was successfully closed.
print("File found!")
except FileNotFoundError:
# Print "File not found!" if a FileNotFoundError occurs when closing the file.
print("File not found!")
Sample Output:
File found!
Explanation:
The said Python code opens a file named 'main.py' in the current working directory using the built-in open() function. The file object is then assigned to the variable "my_file".
After that, a try-except block is used to handle any errors encountered during the file closure process. If the file is successfully closed, the program will print "File found!" to the console.
If the file cannot be closed, a FileNotFoundError will be raised and the code in the except block will be executed. This will print "File not found!" to the console.
Python Code Editor:
Previous: Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).
Next: Write a Python program to determine if a Python shell is executing in 32bit or 64bit mode on OS.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics