Python彻底删除文件夹及其子文件方式

yipeiwu_com6年前Python基础

我就废话不多说了,直接上代码吧!

#coding:utf-8
import os
import stat
import shutil
#filePath:文件夹路径
def delete_file(filePath):
 if os.path.exists(filePath):
  for fileList in os.walk(filePath):
   for name in fileList[2]:
    os.chmod(os.path.join(fileList[0],name), stat.S_IWRITE)
    os.remove(os.path.join(fileList[0],name))
  shutil.rmtree(filePath)
  return "delete ok"
 else:
  return "no filepath"
 
print os.path.exists("E:\\biancheng\\python\\ascas") 
print delete_file("E:\\biancheng\\python\\ascas")

以上这篇Python彻底删除文件夹及其子文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现以时间换空间的缓存替换算法

缓存是指可以进行高速数据交换的存储器,它先于内存与CPU交换数据,因此速度很快。缓存就是把一些数据暂时存放于某些地方,可能是内存,也有可能硬盘。 在使用Scrapy爬网站的时候,产生出来...

python 反向输出字符串的方法

python 反向输出字符串的方法 方法一:采用列表reversed函数 class Solution(object): def reverse_string(self, s):...

python替换字符串中的子串图文步骤

python替换字符串中的子串图文步骤

修改字符串本身是不可能的,因为字符串是不可变类型,只能是通过某些方法来产生它的副本。再把副本赋值给原字符串,达到类似替换的作用。这里介绍几种方法。 旧串换新串:使用str.replace...

pandas数据处理进阶详解

一、pandas的统计分析 1、关于pandas 的数值统计(统计detail 中的 单价的相关指标) import pandas as pd # 加载数据 detail =...

Windows下PyMongo下载及安装教程

PyMongo下载 PyMongo下载地址: http://pypi.python.org/pypi/pymongo/ 当前可下载选项: 复制代码 代码如下: pymongo-1.11....