Как правильно проверить атрибуты доступа файла? То есть доступна ли запись в файл или является ли он исполняемым?
Для этого в 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
🌎 The Patagonian "stone flower" fungus, living on roots in arid South America, mimics pebbles to avoid predation. Its camouflaged fruiting bodies slowly absorb moisture and burst after rare rains, releasing clouds of spores across the steppe. ✨
#fungus⚡#camouflage⚡#Patagonia
👉subscribe Interesting Planet
🌎 The Devil’s Cigar is a rare fungus found only in parts of Texas and Japan. When it opens, it releases visible clouds of spores with a faint hissing sound—one of the few mushrooms that “smokes” as it spreads its seeds! ✨
#fungus⚡#nature⚡#Texas
👉subscribe Interesting Planet
🌎 The leafcutter ant creates underground gardens by carrying fresh leaves to feed a special fungus, which is their true food. This farming partnership has evolved over millions of years, making these ants nature’s original farmers long before humans. ✨
#ants⚡#fungus⚡#agriculture
👉subscribe Interesting Planet