Как правильно проверить атрибуты доступа файла? То есть доступна ли запись в файл или является ли он исполняемым?
Для этого в Python есть функция os.access()
Проверять так:
os.access(path, flag)
Функция вернёт bool в зависимости от наличия указанного флага.
Всего есть 4 флага проверки:
os.F_OK - наличие файла на диске
os.R_OK - доступ на чтение
os.W_OK - доступ на запись
os.X_OK - доступ на исполнение
Например, вместо try-except лучше делать так (пример из документации):
if os.access("myfile", os.R_OK):
with open("myfile") as fp:
return fp.read()
#basic#libs
Live: Get your virtual panda cuddles from Chongqing Zoo!
It's Saturday! Time for some super cute pandas. Yu Ai, Yu Ke, Mang Cancan, Qi Sanmei and Liang Yue in Chongqing Zoo get ready for clumsy rolls, silly play and fluffy cuteness. Join us to have a look! #panda
via CGTN
🩸🅰️🩸🩸🅰️
A Chinese zoo is under fire again for passing off dogs as pandas. This is the third time that people have been tricked by painting ordinary chow chows as pandas.
Visitors began to suspect that they weren't pandas when the spotted furry creatures started barking and panting like dogs.
The plan was perfect. What could go wrong?
#Panda#China
MARKHEMIST