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 生日广州男生祝福语简短