Python验证文件是否可读写代码分享
本文分享实例代码主要在实现验证文件是否有读写权限问题,具体如下:
#Importpythonlibs importos defis_writeable(path,check_parent=False): ''' Checkifagivenpathiswriteablebythecurrentuser. :parampath:Thepathtocheck :paramcheck_parent:Ifthepathtocheckdoesnotexist,checkforthe abilitytowritetotheparentdirectoryinstead :returns:TrueorFalse ''' ifos.access(path,os.F_OK)andos.access(path,os.W_OK): #Thepathexistsandiswriteable returnTrue ifos.access(path,os.F_OK)andnotos.access(path,os.W_OK): #Thepathexistsandisnotwriteable returnFalse #Thepathdoesnotexistsorisnotwriteable ifcheck_parentisFalse: #We'renotallowedtochecktheparentdirectoryoftheprovidedpath returnFalse #Letsgettheparentdirectoryoftheprovidedpath parent_dir=os.path.dirname(path) ifnotos.access(parent_dir,os.F_OK): #Parentdirectorydoesnotexit returnFalse #Finally,returnifwe'reallowedtowriteintheparentdirectoryofthe #providedpath returnos.access(parent_dir,os.W_OK) defis_readable(path): ''' Checkifagivenpathisreadablebythecurrentuser. :parampath:Thepathtocheck :returns:TrueorFalse ''' ifos.access(path,os.F_OK)andos.access(path,os.R_OK): #Thepathexistsandisreadable returnTrue #Thepathdoesnotexist returnFalse
总结
以上就是本文关于Python验证文件是否可读写代码分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:
Python文件操作基本流程代码实例
Python实现读取txt文件并画三维图简单代码示例
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短