语速
Python的文件操作很多,为了方便以后查询使用,在此做下记录。
注:这些函数都可以在官网查询,这里做个cache。
遍历文件
遍历当前文件夹,并且
for file in os.listdir(path): file_path = os.path.join(path, file) print(file_path)
判断文件是否存在
判断文件是否存在,如果不存在,创建新文件夹
if not os.path.exists(path): os.mkdir(path)
删除文件
递归的删除文件
import shutil shutil.rmtree(file)
shutil是一个high-level file operations,因此相对于其他的python接口,更加偏向于人们的习惯性思维。