check file Existing with Python Script

Started by nikolaymartin, April 23, 2020, 09:50:22 AM

Previous topic - Next topic

nikolaymartin

Are there any way to check the file existence in Python script.

Otanx

There is a ".exists" function that will return True. Off the top of my head...

import os
print(os.file.exists("myfile.txt"))

Will print True if file exists, and False if it is missing.

Code may not be 100% but should be close

-Otanx


edizgeorgi

#2
Python provides exists() function which can be used like below. Look https://www.networking-forums.com/programming-goodies-and-software-defined-networking/check-file-existing-with-python-script for more information. 

import os.path 

if(os.path.exists("/etc/passwd")): 

   print("/etc/passwd exists")