Как правильно проверить атрибуты доступа файла? То есть доступна ли запись в файл или является ли он исполняемым?
Для этого в 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
Let's come forward with #JanBhagidari to clean our water bodies and raise awareness about the cleanliness of them🏖️🌊
Take the Puneet Sankalp Pledge to tackle the issue of plastic pollution for a better tomorrow!
Visit: https://pledge.mygov.in/puneet-sankalp/
Your opinion can help us build a #NewIndia of our dreams. Tell us your ideas for Union Budget 2023-24 and join the good governance of #JanBhagidari
Visit: https://www.mygov.in/group-issue/inviting-ideas-and-suggestions-union-budget-2023-2024/
#JanBhagidari के जरिए राष्ट्रीय पोषण मिशन अब एक #JanAndolan बन गया है। इससे महिलाओं, गर्भवती और दूध पिलाने वाली माताओं, बच्चों और किशोरों की पोषण स्थिति में तेजी से सुधार देश की भावी पीढ़ी को सुरक्षित कर रहा है। #PoshanAbhiyaan2023
https://youtu.be/FKkEDtQ15tc
Rashtriya Poshan Mission now becoming a #JanAndolan with the help of #JanBhagidari
It is improving the nutritional status of women, pregnant and lactating mothers, children and adolescents. The moment is securing the future generation of India.
#PoshanAbhiyaan2023
https://youtu.be/FKkEDtQ15tc