当前位置: 首页 > article >正文

在更改文件名字关于PermissionError: [WinError 5] 拒绝访问。

我尝试了更改虚拟环境权限,尝试了更改文件属性为只读都没办法解决

for i in filename_lst:
    if '.' in i:
        filepath = os.path.join(path, i)
        target_dir = os.path.splitext(filepath)[0]  # 移除文件扩展名得到目标目录
        unzip(filepath, target_dir)
        # 假设您的 JSON 数据存储在一个名为 "article.json" 的文件中
        with open(target_dir+"/output.json", "r",encoding='utf-8') as f:
            data = json.load(f)
            # 从 JSON 数据中提取标题
            title = data["title"]
            current_file_name=target_dir
            new_file_name=path+'/'+title
            os.rename(current_file_name, new_file_name)

后来我发现title可能有违法字符

for i in filename_lst:
    if '.' in i:
        filepath = os.path.join(path, i)
        target_dir = os.path.splitext(filepath)[0]  # 移除文件扩展名得到目标目录
        unzip(filepath, target_dir)
        # 假设您的 JSON 数据存储在一个名为 "article.json" 的文件中
        with open(target_dir+"/output.json", "r",encoding='utf-8') as f:
            data = json.load(f)
            # 从 JSON 数据中提取标题
            title = data["title"]
            current_file_name=target_dir
            new_file_name=path+'/'+title
            

import re

def clean_directory_name(dir_name):
    # 移除Windows文件名中不允许的字符
    return re.sub(r'[<>:"/\\|?*]', '', dir_name)

# 假设 title 是从 JSON 中提取的标题
title = "Data Augmentation using LLMs: Data Perspectives, Learning Paradigms and Challenges"
clean_title = clean_directory_name(title)
new_file_name = os.path.join(path, clean_title)

# 现在尝试重命名
try:
    os.rename(target_dir, new_file_name)
except PermissionError as e:
    print(f"Permission denied when renaming {target_dir} to {new_file_name}: {e}")
except Exception as e:
    print(f"An error occurred: {e}")

发现没问题了准备把这个clean函数放入循环

for i in filename_lst:
    if '.' in i:
        filepath = os.path.join(path, i)
        target_dir = os.path.splitext(filepath)[0]  # 移除文件扩展名得到目标目录
        unzip(filepath, target_dir)
        
        # 假设您的 JSON 数据存储在一个名为 "output.json" 的文件中
        json_file_path = os.path.join(target_dir, "output.json")
        if os.path.exists(json_file_path):
            try:
                with open(json_file_path, "r", encoding='utf-8') as f:
                    data = json.load(f)
                    # 从 JSON 数据中提取标题
                    title = data["title"]
                    clean_title = clean_directory_name(title)
                    new_file_name = os.path.join(path, clean_title)
                    
                    # 检查是否已存在同名目录
                    if not os.path.exists(new_file_name):
                        os.rename(target_dir, new_file_name)
                    else:
                        print(f"Directory '{new_file_name}' already exists. Skipping rename.")
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON from {json_file_path}: {e}")
            except PermissionError as e:
                print(f"Permission denied when renaming {target_dir} to {new_file_name}: {e}")
            except Exception as e:
                print(f"An error occurred: {e}")

结果还是PermissionError: [WinError 5] 拒绝访问。

最后发现 可能是with open使得打开了文件夹 所以导致没有办法重命名 就将rename写到open外面

for i in filename_lst:
    if '.' in i:
        filepath = os.path.join(path, i)
        target_dir = os.path.splitext(filepath)[0]  # 移除文件扩展名得到目标目录
        unzip(filepath, target_dir)
        with open(target_dir+"/output.json", "r",encoding='utf-8') as f:
            data = json.load(f)
            # 从 JSON 数据中提取标题
            title = data["title"]
            clean_title=clean_directory_name(title)
            current_file_name=target_dir
            new_file_name=path+'/'+title
            new_file_name = os.path.join(path, clean_title)
        os.rename(current_file_name, new_file_name)

解决


http://www.kler.cn/a/415334.html

相关文章:

  • DeepSpeed框架配置解析:一份详细的日志分析
  • 电子应用设计方案-30:智能扫地机器人系统方案设计
  • 18. 【.NET 8 实战--孢子记账--从单体到微服务】--记账模块--账本
  • eBay 基于 Celeborn RESTful API 进行自动化工具集成实践
  • Flink四大基石之CheckPoint
  • 计算机网络:数据链路层(二)
  • Milvus×Florence:一文读懂如何构建多任务视觉模型
  • 矩阵重构——reshape函数
  • Vue 3 组件通信教程
  • 不同云计算网络安全等级
  • HTTPTomcatServlet
  • Node报错:npm error code ETIMEDOUT
  • 智能合约开发框架--Hardhat
  • 电商数据采集电商行业数据分析电商平台数据获取|保障稳定的API接口数据
  • 如何在CodeIgniter中调用构造函数
  • DataOps 体系对企业数据管理价值及落地的探讨
  • 合并视频文件:使用Python和MoviePy库的简单指南
  • Python 视频合并工具
  • 乐鑫发布 esp-iot-solution v2.0 版本
  • RPC——Remote Procedure Call(远程过程调用)