python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直到删除.但是,我们可以结合多个内置库函数,达到一次删除非空文件夹,不管其目录层次有多深. import os import shutil import traceback import globalvar def misc_init() # clean the test result folder
IndentationError: unindent does not match any outer indentation level 文件未对齐,在记事本打开. 乱码原因:源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了! 解决方法:1.print mystr.decode('utf-8').encode('gbk')2.比较通用的方法: 复制代码代码如下: import systype = sys.getfiles
python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" #指定的目录 for i in os.listdir(dir): #遍历指定目录的文件 newfile = i.replace('a','b') # 替换 oldname = dir +'/'+str(i) newname = dir +'/'+str(newfile) shutil.move(oldnam