python对文件目录的操作方法实例总结
Python  /  管理员 发布于 7年前   178
本文实例讲述了python对文件目录的操作方法。分享给大家供大家参考,具体如下:
python 可以很方便的对文件进行打开,读写操作,删除操作,也可以很方便的对文件夹进行遍历操作。总体说来,有如下几个方面:
1. python 遍历文件目录,当然可以递归
2. python 删除文件
3. python 对文件进行重命名操作
4. python 创建文件夹 (多个层级创建)
5. python 删除文件夹 (多个层级删除)
6. python 移动文件
7. python 查找文件
8. 得到文件夹的大小
下面的代码是我在用python 做一个网盘服务端的时候用到的一些方法,记录下来,以供以后参考.
#coding:utf-8import StringIOimport jsonimport osimport timeimport globimport shutilDATETIMEFORMATER='%Y-%m-%d %X'#only for windowsRECYCLED_FOLDER_NAME='Recycled'def dateformat(datetime): '''return GMT TIME,need to change to LOCAL TIME''' return time.strftime( DATETIMEFORMATER,time.gmtime(datetime) )def filesizeformat(size): ''' Convert file size to string ''' KBSIZE=1024.00 strSize='0 Byte' if (size < KBSIZE): strSize = '%.2f Byte' % (size) elif (size >= KBSIZE and size < KBSIZE**2): strSize = '%.2f K' % (size / KBSIZE) elif (size >= KBSIZE**2 and size < KBSIZE**3): strSize = '%.2f M' % (size / KBSIZE / KBSIZE) elif (size >= KBSIZE**3): strSize = '%.2f G' % (size / KBSIZE / KBSIZE / KBSIZE) return strSizedef listdir(path): if os.path.isfile(path): return '[]' allFiles=os.listdir(path) retlist=[] for cfile in allFiles: fileinfo={} filepath=(path+os.path.sep+cfile).replace("\\","/") if cfile==RECYCLED_FOLDER_NAME: continue if os.path.isdir(filepath): fileinfo['isfile'] = '0' fileinfo['size'] = getfoldersize(filepath) else: fileinfo['isfile'] = '1' fileinfo['size'] = os.path.getsize(filepath) fileinfo['name'] = cfile fileinfo['lastvisittime'] = dateformat( os.path.getatime(filepath) ) fileinfo['createtime'] = dateformat( os.path.getctime(filepath) ) fileinfo['lastmodifytime'] = dateformat( os.path.getmtime(filepath) ) retlist.append(fileinfo) retStr=json.dumps(retlist,encoding='utf-8') return retStrdef deletefile(path): if os.path.exists(path): os.remove(path)def rename(old,new): if os.path.exists(old): os.rename(old, new)def checkoutfile(path): passdef checkinfile(path): passdef lockfile(path): passdef unlockfile(path): passdef createfolder(path): if not os.path.exists(path): os.mkdir(path)def createfolders(path): if not os.path.exists(path): os.makedirs(path);def deletefolder(path): if os.path.isdir(path): os.rmdir(path)def retreeExceptionHandler(fun,path,excinfo): passdef deletefolders(path):# if os.path.isdir(path):# os.removedirs(path) shutil.rmtree(path,ignore_errors=False,onerror=retreeExceptionHandler)def movefile(old,new): shutil.move(old, new)def getfoldersize(path): size = 0 for root, dirs, files in os.walk(path): size += sum([os.path.getsize(os.path.join(root, name)) for name in files]) return sizedef searchfile(path,ext): returnList=glob.glob1(path, ext) return returnListif __name__=='__main__': listdir('c:/vDriver') #searchfile('c:/vDriver','*.log')
上面的代码,根据方法的命名,就可以知道 python 操作文件以及文件夹的各种方法。
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号