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文件并画三维图简单代码示例
如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短