Python驗證文件是否可讀寫代碼分享
更新時間:2017年12月11日 09:46:35 作者:張宋付
這篇文章主要介紹了Python驗證文件是否可讀寫代碼分享,具有一定借鑒價值,需要的朋友可以參考下。
本文分享實例代碼主要在實現(xiàn)驗證文件是否有讀寫權限問題,具體如下:
# Import python libs import os def is_writeable(path, check_parent=False): ''' Check if a given path is writeable by the current user. :param path: The path to check :param check_parent: If the path to check does not exist, check for the ability to write to the parent directory instead :returns: True or False ''' if os.access(path, os.F_OK) and os.access(path, os.W_OK): # The path exists and is writeable return True if os.access(path, os.F_OK) and not os.access(path, os.W_OK): # The path exists and is not writeable return False # The path does not exists or is not writeable if check_parent is False: # We're not allowed to check the parent directory of the provided path return False # Lets get the parent directory of the provided path parent_dir = os.path.dirname(path) if not os.access(parent_dir, os.F_OK): # Parent directory does not exit return False # Finally, return if we're allowed to write in the parent directory of the # provided path return os.access(parent_dir, os.W_OK) def is_readable(path): ''' Check if a given path is readable by the current user. :param path: The path to check :returns: True or False ''' if os.access(path, os.F_OK) and os.access(path, os.R_OK): # The path exists and is readable return True # The path does not exist return False
總結
以上就是本文關于Python驗證文件是否可讀寫代碼分享的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python實現(xiàn)讀取txt文件并畫三維圖簡單代碼示例
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
python3+PyQt5圖形項的自定義和交互 python3實現(xiàn)page Designer應用程序
這篇文章主要為大家詳細介紹了python3+PyQt5圖形項的自定義和交互,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04

