Python, how to check if a file or directory exists

The os.path.exists() method provided by the os standard library module returns True if a file exists, and False if not.

Here is how to use it:

import os

filename = '/Users/flavio/test.txt'

exists = os.path.exists(filename)

print(exists) # True


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

The os.path.exists() method provided by the os standard library module returns True if a file exists, and False if not.

Here is how to use it:

import os

filename = '/Users/flavio/test.txt'

exists = os.path.exists(filename)

print(exists) # True


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-01-25T05:00:00+00:00) Python, how to check if a file or directory exists. Retrieved from https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/

MLA
" » Python, how to check if a file or directory exists." flaviocopes.com | Sciencx - Monday January 25, 2021, https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/
HARVARD
flaviocopes.com | Sciencx Monday January 25, 2021 » Python, how to check if a file or directory exists., viewed ,<https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/>
VANCOUVER
flaviocopes.com | Sciencx - » Python, how to check if a file or directory exists. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/
CHICAGO
" » Python, how to check if a file or directory exists." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/
IEEE
" » Python, how to check if a file or directory exists." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/. [Accessed: ]
rf:citation
» Python, how to check if a file or directory exists | flaviocopes.com | Sciencx | https://www.scien.cx/2021/01/25/python-how-to-check-if-a-file-or-directory-exists/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.