كيف يمكنني معرفة ما إذا كان الملف موجودًا أم لا ، دون استخدام بيان try
؟
كيف أتحقق من وجود ملف في python؟
abdullah
#2
استعمل os.path.isfile()
مع os.access()
:
import os
import os.path
PATH='./file.txt'
if os.path.isfile(PATH) and os.access(PATH, os.R_OK):
print "File exists and is readable"
else:
print "Either the file is missing or not readable"