Python: Check whether a file exists using Python
Python Basic: Exercise-41 with Solution
Write a Python program to check whether a file exists.
Sample Solution:-
import os.path
open('abc.txt', 'w')
print(os.path.isfile('abc.txt'))
if os.path.isfile("C:\test.txt"):
print ("Is File")
else:
print ("Is Not File")
if os.path.isfile(r"\\full\uncpath\test.txt"):
print ("Is File")
else:
print ("Is Not File")
print ("Is File")
else:
print ("Is Not File")
============ RESTART: F:/Python_APSC/py-ex-basic-41.py ============
True
Is Not File
Is Not File
Is Not File
>>>
沒有留言:
張貼留言