Python performs batch processing of CMD commands on files

Get multiple model files, then compress each file with the cmd command, and then re-export to a new directory.
This code is only for temporary use, not perfect, and some codes and logic are not scientific. The purpose of this article is mainly for personal records, so that it can be pasted and used later. Please forgive me if there are any unsatisfactory places.

import cmd, sys
from turtle import *
import os 
import shutil
import asyncio

# 想试试异步删除文件,但好像没成功。目前能用就没继续完善了。
async def del_dic(processed_path):
    # 查看是否有目录存在,有的话,先删除(包括里面的所有文件),然后再创建新的。
    isExists=os.path.exists(processed_path)
    if isExists:
        shutil.rmtree(processed_path)
    os.makedirs(processed_path) 

# 把glb模型文件进行 drc压缩处理
async def glb_to_drc(file_dir):   
    # 压缩后的文件需要存放的目录
    processed_path = file_dir + "/"+"Processed"

    # 获取这个路径下所有的文件和文件夹
    files = os.listdir(file_dir)
    
    task = asyncio.create_task(del_dic(processed_path))
    await task
    
    glb_length = 0
    for file_name in files:
        file_name_only, file_extension = os.path.splitext(file_name)
        if(file_extension == '.glb'):
            # 执行 CMD命令
            cmd = "gltf-pipeline -d -i %s -o %s"%((file_dir+'/'+file_name),(processed_path+'/'+file_name_only+'-processed'+file_extension))
          
            os.system(cmd)
            glb_length+=1
    print("%s glb 模型数量 %s "%(file_dir , glb_length)) 

async def files_pos():  
   await glb_to_drc('/Users/edwinlee/Downloads/xxxxxx')
   await glb_to_drc('/Users/edwinlee/Downloads/xxxxxx2')
   await glb_to_drc('/Users/edwinlee/Downloads/xxxxxx3')
   await glb_to_drc('/Users/edwinlee/Downloads/xxxxxx4')


asyncio.run(files_pos())

Guess you like

Origin blog.csdn.net/KiTok/article/details/122690206