Python isfile example

Python isfile example Tutorial: The OS module will provide functions for interacting with the operating system. They will provide modules and portable way of using operating system dependent functionality. The Os.path module is a sub module of OS module in Python that is used for common path name manipulation. The os.path.isfile () method in Python is used to check the specified path is an existing regular file or not.
Syntax:-
 os.path.isfile (path)
Parameter: -
path:-
The path-like object represents a file system path as it is a string object representing a path.
Return Type:-
The method will return a Boolean value of class bool and true if specified path is an existing regular file/false.
Example:-
import os
path = '/home/User/Desktop/file.txt'
IsFile = os.path.isfile (path)
print (isFile)
path = '/home/User/Desktop/'
IsFile = os.path.isfile (path)
print (isFile)
Output:-
True
False

os.path.isfile ():-

We use the isfile command to check a given input is a file/directory.
Import os.path
From os import path
Def main ():
Print(“Is it file?”+str (path.isfile (‘guru99.txt’)))
Print(“Is it file?”+str (path.isfile (‘mydirectory’)))
If_name_==”_main_”:
Main()
Output:-
Is it File? True
Is it File? False

Using pathlib module:-

It is the object-oriented approach by using the os module.
The class in the pahlib is Path has a function is_file () and returns True if the Path is pointing to a regular file.
If you are using the symbolic link that points to a regular file then it returns true.
Example:-
from pathlib import Path
chk_file = Path ("demo/testing.txt")
if chk_file.is_file ():
 chk_file.unlink ()
 print ("File Deleted!")
 else:
 print ("File does not exist!")

Using os.path isfile function:-

The checking file exists by using the os module’s path class.
The Path class has a method isfile that returns true if the path is a regular file.


 

Example:-
     import os
    CheckFile = "demo/testfile.txt"
    if os.path.isfile (checkFile):
    os.remove (checkFile)
    print ("The File is removed!"
    else
    print ("Specified file does not exist!")

Os.Path exists function:-

The os.path has function called exists () that returns True if the path is an actual file.
The function takes an argument which is the path of the file.
The function will return False if the path does not exist also returns False in some platforms if the requested file is not granted to execute.
Example:-
import os
FilePath = "demo/tst-exist.txt"
if os.path.exists (filePath):  
os.remove (filePath)
 print ("The File is removed!")
 else:
 print ("File not found!")

By exception handling:-

We avoid crashing a program if the specified file is not found.
And execute a command to open a file and this is not found the program raises an
Example:-
filepath = “‘demp/test.txt”
file_tst = open (filepath)
file_tst.close ()
Example:-
filepath = "demp/test.txt"
try:
file_tst = open (filepath)
 file_tst.close ()
 except FileNotFoundError:
 Print ('File does not exist')
Example:-
Import os.path
Results=os.path.isfile (‘example.py’)
Print (results)
Results=os.path.isfile (‘.’)
Print (results)

Output:-

$python example.py True False

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India