العودة إلى موقع برمج

كيف أتحقق من وجود ملف في python؟

python
#1

كيف يمكنني معرفة ما إذا كان الملف موجودًا أم لا ، دون استخدام بيان try ؟

#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"