File does not exist Python

In this python tutorial, we will discuss the File does not exist python, and also we will cover the below topics:

  • File does not exist Python
  • Python check if the file does not exist and create
  • File does not exist python exception
  • Python Check If a file exists
  • If the file does not exist python
  • Python file does not exist error
  • Python if the file does not exists skip
  • Python raise file does not exists

File does not exist in Python

Here, we can see how to check whether file exists in Python.

  • In this example, I have imported a module called os.path. The os.path module is used for processing the files from different places in the system.
  • The os.path.exists is used to check the specified path exists or not.
  • The path of the file is assigned as r’C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\mobile.txt’. The mobile.txt is the name of the file.

Example:

import os.path
print(os.path.exists( r'C:\Users\Administrator.SHAREPOINTSKY\Desktop\Work\mobile.txt'))

As the file is not present so the output is returned as False. You can refer to the below screenshot for the output.

File does not exist python
File does not exist python

This is how to fix file not exist error python.

Read, Python program to print pattern.

Python check if file does not exists and create

Now, we can see how to check if a file does not exist and create it in Python.

  • In this example, I have imported a module called os. The path of the file is read.
  • The if condition is used as os.path.exists(x), os.path.isfile(x).
  • If the file is present the condition returns true as the output.
  • If the file is not present then the else condition is executed and the file is created by using f = open(“pic.txt”,”w”). The pic.txt is the name of the file and “w” is the mode of the file.
  • The f.close() is used to close the file.

Example:

import os
x=r'C:\Users\Administrator.SHAREPOINTSKY\photo.txt'
if os.path.exists(x):
    if os.path.isfile(x):
        print("file is present")
else:
  print("file is not present and creating a new file")
f = open("pic.txt","w")
f.close()

As the file is not present the else condition is executed and the new file is created. You can refer to the below screenshot for the output.

Python check if file does not exists and create
Python check if file does not exists and create

This is how to check if file does not exists and create it in Python.

File does not exist python exception

Here, we can see file does not exist exception in python.

  • In this example, I have used exceptions.
  • Here, I have taken a try block to check whether the file exists or not. So I have opened the file as with open(“bottle.py”) as f if condition is true it should print(“File present”).
  • If the file is not found, the except FileNotFoundError is used. As the file is not present exception is executed.

Example:

try:
    with open("bottle.py") as f:
        print("File present")
except FileNotFoundError:
    print('File is not present')

You can refer to the below screenshot for the output.

File does not exist python exception
File does not exist python exception

Python check If a file exists

Now, we can see check if a file exists in python.

  • In this example, I have imported a module called os.path and also a path from os. The module path checks the specified path is present or not.
  • I have used a function called main as def main().
  • The path.exists() is used to check whether the specified path exists or not. as both the file are not present.
  • As only one file is present we can see that the output is true and another file 925.txt is not found so it returns false as the output.
  • Every module has an attribute called __name__., the value of the attribute is set to “__main__”.

Example:

import os.path
from os import path
def main():
   print (str(path.exists('pic.txt')))
   print (str(path.exists('925.txt')))
if __name__== "__main__":
   main()

As the file 925.txt file is not present, we can see the false is returned as the output. The below screenshot shows the output.

Python check If file exists
Python check If file exists

This is how to check if a file exists in Python or not.

If the file does not exist python

Now, we can see if the file does not exist in python.

  • In this example, I have imported a module called pathlib. The module pathlib is used to work with files and directories.
  • The pathlib.path is used to join the path of the two directories.
  • The path.exists() is used to check whether the file exists are not.
  • The path.is_file is used to find the result of the file.

Example:

import pathlib
path = pathlib.Path('laptop.txt')
path.exists()
print(path.is_file())

As the file is not present false is returned as the output. You can refer to the below screenshot for the output.

If the file does not exist python
If the file does not exist python

Python file does not exist error

Here, we can see file does not exist error in python.

In this example, I have taken a file as mobile.txt to read the file as the file does not exists so the error is found.

Example:

myfile = open("mobile.txt")
print(myfile.read())
myfile.close()

We can see FileNotFoundError as the output. You can refer to the below screenshot for the output. In order to solve the error, we have to give the file name which is present in the system.

Python file does not exist error
Python file does not exist error

Python if the file does not exists skip

  • In this example, I have used exceptions, So I have taken try block to check whether the file exists or not.
  • I have opened the file as with open(“bike.txt”) as f, if condition is true it should print(“File present”).
  • If the file is not found, the except FileNotFoundError is used. As the file is not present exception is executed and skipped to print(‘File is not present’).

Example:

try:
    with open("bike.txt") as f:
        print("File present")
except FileNotFoundError:
    print('File is not present')

The except is executed so the output will be File is not present. You can refer to the below screenshot for the output.

Python if the file does not exists skip
Python if the file does not exists skip

Python raise file does not exist

Here, we can see how to raise file does not exist in python.

  • In this example, I have imported a module called os. The os module establishes the connection between the user and the operating system.
  • If the file is not present it should raise an error as FileNotFoundError.
  • The keyword raise is used to raise the exception.

Example:

import os
if not os.path.isfile("text.txt"):
    raise FileNotFoundError

As the file is not present an error is raised as the output. You can refer to the below screenshot for the output.

Python raise file does not exists
Python raise file does not exists

You may like the following Python tutorials:

In this tutorial, we have learned about File does not exist python, and also we have covered these topics:

  • File does not exist python
  • Python check if the file does not exist and create
  • File does not exist python exception
  • Python Check If a file exists
  • If the file does not exist python
  • Python if the file does not exists skip
  • Python raise file does not exists