Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Python»Python – Check If File or Directory Exists

    Python – Check If File or Directory Exists

    RahulBy RahulApril 4, 20172 Mins ReadUpdated:May 8, 2020

    The Python os.path module is used for the file or directory pathename’s manipulations. The method isfile() of this module is used to check if any file is available or not. Similarly exists() function returns true for files and directory exists.

    #1. Python – Check if File Exists

    For example, To test how isfile() and exists() functions work. Create a TestFile.py file using following content and execute it python.

    • isfile() – function check if given input file exists and is a file not directory.
    • exists() – function check if given input file exists. It doesn’t matter its a file or directory.

    See the below examples:

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    import os.path
     
    print os.path.isfile("/etc/hosts")       #True
    print os.path.isfile("/etc")             #False
    print os.path.isfile("/does/not/exist")  #False
    print os.path.exists("/etc/hosts")       #True
    print os.path.exists("/etc")             #True
    print os.path.exists("/does/not/exist")  #False

    Python >= 3.4 users can use object oriented approach to check if file exist or not. we need to import Path from the pathlib module.

    Python
    1
    2
    3
    4
    5
    6
    7
    8
    from pathlib import Path
     
    fileName = Path("/etc/hosts")
     
    if fileName.is_file():
        print ("File exist")
    else:
        print ("File not exist")

    #2. Python – Check if File Readable

    You can also check if file exists and readable for current users in Python.

    Python
    1
    2
    3
    4
    5
    6
    import os.path
     
    if os.path.isfile('/etc/hosts') and os.access('/etc/hosts', os.R_OK):
        print "File exists and is readable"
    else:
        print "Either file is missing or is not readable"

    #3. Python – Check if Link File

    Use os.path.islink to find if any file is a link file.

    Python
    1
    2
    3
    4
    5
    6
    import os.path
     
    if os.path.isfile("/etc/hosts") and os.path.islink("/etc/hosts"):
        print "This is a link file"
    else:
        print "This is actual file"

    #4. Python – Create Directory if not Exists

    Use os.path.exists to check if any directory exists or not and use os.makedirs to create a directory. Below example will create directory /tmp/newdir if not exists.

    Python
    1
    2
    if not os.path.exists('/tmp/newdir'):
        os.makedirs('/tmp/newdir')

    Python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Upgrade to Ubuntu 17.04 (Zesty Zapus)
    Next Article How to Prevent SQL Injection in PHP

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    How To Install Python 3.10 on Debian 11/10

    2 Mins Read

    How to Install Python 3.10 on CentOS/RHEL 8 & Fedora 35/34

    Updated:February 12, 20223 Mins Read

    How To Install Python 3.10 on Ubuntu, Debian & Linux Mint

    Updated:April 22, 20222 Mins Read

    How to Switch Python Version in Ubuntu & Debian

    Updated:April 22, 20223 Mins Read

    Working with Python Functions

    6 Mins Read

    5 Comments

    1. Frank Koslowski on March 14, 2020 1:19 am

      Very good and straight forward advise on what should be a simple thing.

      Reply
    2. Chris on October 9, 2019 7:25 pm

      Many thanks, Rahul!

      Reply
    3. Kiran on April 29, 2019 3:02 pm

      Thanks, Rahul for precise and good explanation.

      Reply
    4. Raghu on February 22, 2019 8:03 am

      Thanks Rahul..

      Reply
    5. Ansh on May 11, 2017 2:43 pm

      Thanks

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.