1、创建文件夹:

os.makedirs(path)和os.mkdir(path)的区别在于:
第一个会创建多级文件夹目录
而后面只会创建一级的目录

这个比较抽象,但意思就是说,如果你所写的文件路径从某个文件夹下开始,就没有相对应的文件夹了。那么,用第一个makedirs()就会全部都创建出来。但是用第二个就是报错。
因为第二个只会创建一级文件夹。

2、删除文件:

os.unlink(path)。这个要指向特定的文件。如果是文件夹,就会报错。
主要用途是删除文件

===========================================================

3、编者按

有时候我们使用shutil.rmtree()、os.rmdir()、os.remove()删除文件时会报[WinError 5] 拒绝访问的错误:
如使用os.remove()删除当前文件夹下的'PackageCache\\com.unity.textmeshpro@1.3.0\\Tests\\Editor.meta'时提示

[WinError 5] 拒绝访问。: ‘PackageCache\com.unity.textmeshpro@1.3.0\Tests\Editor.meta’

这个时候我们可以让Python运行cmd命令强制删除此文件:

import os
os.system('del "PackageCache\com.unity.textmeshpro@1.3.0\Tests\Editor.meta" /F')

关于使用CMD命令删除文件或文件夹,可以参考:Windows CMD删除文件或文件夹命令帮助

4. 运用

import os
import shutildirs = ['.idea', '.vs', 'Logs', 'obj']
files = ['.sln', '.csproj']dirsCnt = 0
filesCnt = 0def delWithCmd(path):try:if os.path.isfile(path):cmd = 'del "'+ path + '" /F'print(cmd)os.system(cmd)except Exception as e:print(e)def deleteDir(dirPath):global dirsCntglobal filesCntfor root, dirs, files in os.walk(dirPath, topdown=False):for name in files:try:filesCnt += 1filePath = os.path.join(root, name)print('file deleted', filesCnt, filePath)os.remove(filePath)except Exception as e:print(e)delWithCmd(filePath)for name in dirs:try:os.rmdir(os.path.join(root, name))dirsCnt += 1except Exception as e:print(e)os.rmdir(dirPath)def delDir(dirPath):global dirsCntshutil.rmtree(dirPath)dirsCnt += 1print('dir deleted', dirsCnt, dirPath)def delFile(filePath):global filesCntos.remove(filePath)filesCnt += 1print('file deleted', filesCnt, filePath)def delete(path):try:if os.path.isfile(path):delFile(path)elif os.path.isdir(path):deleteDir(path)except Exception as e:print(e)for proj in os.listdir():if not os.path.isdir(proj):continueos.chdir(proj)print(os.getcwd())for p in os.listdir():if os.path.isdir(p) and p in dirs:delete(p)elif os.path.isfile(p) and os.path.splitext(p)[1] in files:delete(p)libPath = 'Library'if os.path.exists(libPath) and os.path.isdir(libPath):os.chdir(libPath)for p in os.listdir():if p == 'LastSceneManagerSetup.txt':continuedelete(p)os.chdir('..')os.chdir('..')

————————————————
原文链接:https://blog.csdn.net/COCO56/article/details/107061932/

————————————————
原文链接:https://blog.csdn.net/a19990412/article/details/79512408

Python 删除只读文件/文件夹【报错】相关推荐

  1. windows python删除文件文件夹报错:PermissionError

    windows python删除文件文件夹报错:PermissionError 我遇到的问题 问题分析 定位 查明原因 解决方案 我遇到的问题 在windows中,使用shutil.rmtree(my ...

  2. python跳过错误_Pandas之read_csv()读取文件跳过报错行的解决

    读取文件时遇到和列数不对应的行,此时会报错.若报错行可以忽略,则添加以下参数: 样式: pandas.read_csv(***,error_bad_lines=False) pandas.read_c ...

  3. maven错误:unknow文件夹--报错:Failure to find xxx:pom:unknown

    maven错误:unknow文件夹--报错:Failure to find xxx:pom:unknown 我的问题原因 我的解决 具体过程 解决 我的问题原因 根本原因:这个依赖的版本号没有指定 我 ...

  4. MyEclipse在删除文件后servers报错问题解决

    MyEclipse在删除文件后servers报错"Could not create the view: An unexpected exception was thrown." 解 ...

  5. tar命令打包压缩时排除.svn .git .repo等特定文件/文件夹报错/未生效

    tar命令打包压缩时排除.svn .git .repo等特定文件/文件夹报错/未生效 当使用tar命令打包压缩的同时期望排除某类文件或文件夹时,可以使用--exclude参数. 在ubuntu20.0 ...

  6. Linux CentOS7 中 完美解决VMTools失效,windows 与 Liunx间完美复制文件,无报错的解决方案

    Linux CentOS7 中 完美解决VMTools失效,windows 与 Liunx间完美复制文件,无报错的解决方案 DreamFollowerss 2020-05-25 12:03:08  3 ...

  7. php删除垃圾文件,Python删除windows垃圾文件的方法

    本文实例讲述了Python删除windows垃圾文件的方法.分享给大家供大家参考.具体如下: #coding:utf-8 import os #from glob import glob if os. ...

  8. springboot文件上传报错:Required request part ‘file‘ is not present

    文件上传报错:Required request part 'file' is not present 如下使用CommonsMultipartResolver: @Configuration publ ...

  9. Mac根目录下无法创建文件或目录-报错 mount_apfs: volume could not be mounted: Operation not permitted mount: / fail

    Mac根目录下无法创建文件或目录- 报错 /data: Read-only file system 报错 mount_apfs: volume could not be mounted: Operat ...

  10. PPT在线预览 转换为图片实现方案 Apache POI 实现时踩坑:含嵌入文件ppt转换报错 ArrayStoreException

    前言 背景:最近项目需要实现PPT预览功能,以及项目APP上浏览ppt,初步方案是ppt转为图片. 实现 1.ppt转为pdf,然后pdf转为图片 该种实现,先将ppt转为pdf文件,实现方式有很多, ...

最新文章

  1. 2018 年最引人注目的科学时刻,《科学》杂志选出14 张年度最佳科学照片
  2. 长途每分钟只要9分钱!
  3. python中常见的流程结构-【Python2】04、Python程序控制结构
  4. 总结一些常用Android adb 命令
  5. IOS开发学习记录第3天之C语言学习
  6. python多进程管道通信(精)
  7. matlab中表示拉普拉斯分布_神奇的正态分布
  8. SQL Server应用程序中的高级SQL注入[转]
  9. C/C++ 编程中多国语言处理
  10. tar.xz如何解压:linux和windows下tar.xz解压命令介绍
  11. 没了珊瑚虫你用谁?八大QQ主流修改版大比拼
  12. Python爬取句子迷-莎士比亚语录
  13. IllegalStateException: For MAC signing you do not need to specify the verifier key separately异常解决
  14. 至联云讲解《关于IPFS我们应该知道哪些真相?》
  15. 《格鲁夫给经理人的第一课》读书笔记
  16. Arcgis常用功能 Python脚本
  17. 【StyleGAN代码学习】StyleGAN模型架构
  18. map的嵌套使用方法
  19. 服务器运维系统哪个好用,可以说宝塔是史上最好用的服务器运维控制面板
  20. 50个好用的前端框架,千万收好以留备用!

热门文章

  1. 转码/解锁工具-CleverPDF
  2. 碰到的bug,解决方法
  3. tcga数据下载_手把手帮你分析这些TCGA甲基化内容
  4. vulnstack-Web安全入门进阶实战靶场配置
  5. MT5:挂单的六种类型
  6. win10开机蓝屏_电脑用户遇到蓝屏ACPIBIOSError应该如何解决?
  7. 【Java】idea找不到符号找不到类,但是却没有错误
  8. 基于深度学习的视线跟踪--数据集
  9. android wifi adb调试,[Android] 无线adb调试
  10. 【论文速递】ACL 2021-CLEVE: 事件抽取的对比预训练