Python 删除只读文件/文件夹【报错】

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 shutil


dirs = ['.idea', '.vs', 'Logs', 'obj']
files = ['.sln', '.csproj']

dirsCnt = 0
filesCnt = 0

def 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 dirsCnt
    global filesCnt
    for root, dirs, files in os.walk(dirPath, topdown=False):
        for name in files:
            try:
                filesCnt += 1
                filePath = 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 += 1
            except Exception as e:
                print(e)
    os.rmdir(dirPath)

def delDir(dirPath):
    global dirsCnt
    shutil.rmtree(dirPath)
    dirsCnt += 1
    print('dir deleted', dirsCnt, dirPath)


def delFile(filePath):
    global filesCnt
    os.remove(filePath)
    filesCnt += 1
    print('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):
        continue
    os.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':
                continue
            delete(p)
        os.chdir('..')
    os.chdir('..')

 

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值