首页 > 其他分享 >***MJ根据表格重命名代码(建立txt)

***MJ根据表格重命名代码(建立txt)

时间:2024-05-27 15:46:35浏览次数:16  
标签:重命名 MJ subdir input path folder txt os

import os
import shutil
from openpyxl import load_workbook

def create_txt_in_subfolder(subfolder_path, content):
    txt_filename = os.path.basename(subfolder_path) + ".txt"
    txt_path = os.path.join(subfolder_path, txt_filename)
    with open(txt_path, 'w', encoding='utf-8') as txt_file:
        txt_file.write(content)

def main():
    excel_path = input("请输入参照的excel表格地址: ")
    root_dir = input("请输入文件夹总地址: ")
    target_dir = input("请输入文件夹移动路径: ")
    input_col = int(input("请输入excel表格的输入列(输入数字): "))
    output_col = int(input("请输入excel表格的输出列(输入数字): "))

    try:
        wb = load_workbook(excel_path)
        sheet = wb.active
    except Exception as e:
        print(f"加载Excel文件失败: {e}")
        return

    folder_info = {}

    for row in sheet.iter_rows(min_row=2, values_only=True):
        input_value = str(row[input_col-1]).strip()
        output_value = str(row[output_col-1]).strip()
        folder_info[input_value] = output_value

    unmatched_folders = []

    for subdir in os.listdir(root_dir):
        subdir_path = os.path.join(root_dir, subdir)
        if os.path.isdir(subdir_path):
            if subdir in folder_info:
                content = folder_info[subdir]
                create_txt_in_subfolder(subdir_path, content)
                print(f"在文件夹{subdir}中创建了txt文件。")
            else:
                unmatched_folders.append(subdir_path)

    if unmatched_folders:
        os.makedirs(target_dir, exist_ok=True)
        for folder in unmatched_folders:
            try:
                shutil.move(folder, target_dir)
                print(f"未匹配的文件夹{os.path.basename(folder)}已移动到{target_dir}。")
            except Exception as e:
                print(f"移动文件夹{os.path.basename(folder)}时发生错误: {e}")

    print("处理完成。")

if __name__ == "__main__":
    main()

 

标签:重命名,MJ,subdir,input,path,folder,txt,os
From: https://www.cnblogs.com/zly324/p/18215675

相关文章

  • VOC数据格式转YOLO(xml转txt)
            近日想训练yolov8模型,在网上找了很久公开数据集,好多都没有数据标注,好不容易找到一篇好的数据集,但是都是xml格式的,因此得想办法把xml格式转为yolo格式。    首先观察xml数据集格式:我圈出来比较重要的几条filename:就是xml标签所对应的图片的名字wid......
  • 创建一个txt文件
    在Python中创建一个文本文件(.txt)非常简单,你可以使用内置的文件操作函数。下面是一个基本示例,展示如何创建一个名为example.txt的空文本文件,并写入一些内容:#打开(或创建)一个文件用于写入withopen('example.txt','w')asfile:#写入一行文本file.write("这是文件中......
  • clion的cMakeList.txt的配置
    保留第一第二行,从第三行开始,使用下面内容替换: #使用此CMakeList时,若要新建C++文件,请按照以下步骤:#1.右键根目录——新建——C/C++源文件#2.在弹出的对话框中,输入文件名(英文小写及下划线),后缀为".cpp",不要勾选“添加到目标”,点击确定。#3.点击左上角横线——文件......
  • git之fatal: unable to access ‘https://11.2.7.8:68/mjgn/mj.git/‘: SSL certifica
    MENU前言方法一方法二方法三方法四结束语前言出现这个错误是因为Git在访问仓库时遇到了SSL证书问题,具体是证书的密钥太弱,不符合安全要求。方法一升级或更换SSL证书联系服务器管理员,确保服务器上使用的是强密钥SSL证书。推荐使用至少2048位的密钥。方法二......
  • vscode使用colcon build编译ros2工程时报错:The current CMakeCache.txt directory...i
    之前已经编译好了一个文件夹A下的工程然后复制出一个文件夹B,再次编译时出现了问题,报错ThecurrentCMakeCache.txtdirectory...isdifferentfrom...其实也能猜到就是当路径从A变到B,不匹配导致报错,但是不知道应该在哪里改CSDN上有个文章给出回答:删除build文件夹,当然把log和ins......
  • Ubuntu下(跨机)文件、文件夹的拷贝、删除、重命名、移动、备份
    同机操作拷贝命令格式:cp-r源文件目的文件1示例操作:cp-r/home/folderA/usr参数r是指连同源文件中的子目录一同拷贝,就把folderA拷贝到了usr下面2示例操作:cp-r/home/folderA/*/usr/folderA操作将文件夹/home/folderA下面的所有文件全部拷贝到了/usr/folderA......
  • idea中的springboot项目如何重命名而不报错
    在IntelliJIDEA中重命名SpringBoot项目需要一些步骤,以确保项目在重命名后不会报错。以下是详细的步骤指南:1.重命名项目文件夹关闭项目:在IntelliJIDEA中关闭当前项目。重命名文件夹:在文件系统中找到项目所在的文件夹,右键重命名文件夹。重新打开项目:在IntelliJID......
  • 在CMakeLists.txt中添加源文件的几种方法
    单个.cpp源文件的情况用这段代码进行测试,CMake中添加一个可执行文件作为构建目标:#include<cstdio>intmain(){printf("Hello,world!\n");}指定源文件的时候可以有两种方式。在创建目标的时候直接指定源文件add_executable(mainmain.cpp)先创建目标,再添加源......
  • [20240515]vim bccalc_XXX.vim使用插件简介.txt
    [20240515]vimbccalc_XXX.vim使用插件简介.txt--//这是我改写vim.org网站的一个调用bc做计算的一个插件bccalc.vim,感觉自己越写越复杂.做一个介绍,便于自己查阅.--//另外注意如果选择多行,结尾要像C语言一样使用分号(;).--//我已经统一在selectvisualnormal模式都是<leader>作......
  • [20240515]建立完善t2sh.sql脚本.txt
    [20240515]建立完善t2sh.sql脚本.txt--//建立一个简单输入简单文本计算sql_id,hash_values的sql脚本,主要用于简单sql语句以及对象的full_hash_value,sql_id,hash_value计算.--Copyright2023lfree.Allrightsreserved.--LicensedundertheApacheLicense,Version2.0.Se......