(24)Python实现递归生成或者删除一个文件目录及文件

  1. import os,errno
  2. #基本工具类
  3. #①递归生成输入的路径下面的文件夹或文件
  4. #②递归删除输入的路径下面的文件夹及文件
  5. ‘‘‘
  6. param : dirPath
  7. return :
  8. AuthorCreated by Wu Yongcong 2017-8-17
  9. function:remove a input dirPath and the files/dictionary under it
  10. ‘‘‘
  11. def removeDir(dirPath):
  12. if not os.path.isdir(dirPath):
  13. return
  14. files = os.listdir(dirPath)
  15. try:
  16. for file in files:
  17. filePath = os.path.join(dirPath, file)
  18. if os.path.isfile(filePath):
  19. os.remove(filePath)
  20. elif os.path.isdir(filePath):
  21. removeDir(filePath)
  22. os.rmdir(dirPath)
  23. except Exception as e:
  24. print(e)
  25. ‘‘‘
  26. param: dirPath
  27. Created by Wu Yongcong 2017-8-17
  28. function:add a input dirPath and the files/dictionary under it
  29. ‘‘‘
  30. def mkdir_p(dirPath):
  31. try:
  32. os.makedirs(dirPath)
  33. except OSError as oe:
  34. if oe.errno == errno.EEXIST and os.path.isdir(dirPath):
  35. pass
  36. else:
  37. raise
时间: 2025-01-08 03:04:48

(24)Python实现递归生成或者删除一个文件目录及文件的相关文章

C++ 删除一个文件目录下的所有文件以及目录

1 //删除一个文件目录下的所有文件,并删除当前文件夹 chrDirName = _T("c:\\123"); 2 BOOL CIVMSvrInfo::ReleaseDirectory(CString chrDirName) 3 { 4 BOOL bRemove = DoRemoveDirectory(chrDirName); 5 if (bRemove) 6 ::RemoveDirectory(chrDirName); 7 else 8 return FALSE; 9 10 retu

FileTest 删除一个目录或文件的方法

/** * 删除一个目录或文件的方法 * */ public class FileTest { public static void main(String[] args) { File dir = new File("a"); //删除a目录 delete(dir); System.out.println("删除完毕!"); } public static void delete(File file) { //1.判断file是文件还是目录? if(file.is

C# TreeView 树节点:递归显示整个系统盘符文件目录及文件

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace Demo { /// <summary> /// 有关对于TreeView节点的填充的相关处理类 /// </summary> class TreeViewUtils { #region 有关将整个系统盘

Python封装函数:实现删除一个list里面的重复,且元素顺序要与原list顺序对应

封装函数:实现删除一个list里面的重复,且元素顺序要与原list顺序对应 代码:def info(l):l1 = l[:]for i in range(len(l)):v = l.count(l[i])if l1.count(l[i]) > 1:for j in range(1, v):l1.remove(l[i])return l1 print(info([1, 2, 3, 4, 2, 3, 6, 2])) 思想:统计相同元素出现的次数,然后删除到1 原文地址:http://blog.51c

如何删除一个顽固的文件(win)

有些软件要强行删除才可以可以参考下面的:一.常规解决办法 1.注消或重启电脑,然后再试着删除. 2.进入“安全模式删除”. 3.在纯DOS命令行下使用DEL.DELTREE和RD命令将其删除. 4.如果是文件夹中有比较多的子目录或文件而导致无法删除,可先删除该文件夹中的子目录和文件,再删除文件夹. 5.如果是在“添加或删除程序”选项卸载一个软件后,发现软件的安装目录仍旧存在,里边残留着几个文件,直接删除时,系统却提示文件正在使用无法删除. 此时可首先打开“命令提示符”窗口,按“Ctrl+Alt+

JAVA利用递归的方法删除一个文件夹以及文件夹下所有的子文件

public static boolean deleteFolder(String url) { File file = new File(url); if (!file.exists()) { return false; } if (file.isFile()) { file.delete(); return true; } else { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { Stri

C#控制台基础 directory删除一个空的文件夹

1 代码 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace directory创建一个文件夹 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14

Python 实现指定目录下 删除指定大小的文件

import os, sys from stat import * BIG_FILE_THRESHOLD = 6000L #1000000L dict1 = {} # dict2 = {} # def treewalk(path): try: for i in os.listdir(path): mode = os.stat(path+"/"+i).st_mode if S_ISDIR(mode) <> True: filename = path+"/"

删除一个文件或多个目录的方法?

/** *    删除一个目录或文件的方法 * */ public class FileTest { public static void main(String[] args) { File dir = new File("a"); //删除a目录 delete(dir); //补充delete方法 System.out.println("删除完毕!"); } public class FileTest { public static void main(Stri