python脚本

开跑脚本

import sys, shutil, configparser
import pandas as pd
from pathlib import Path

# 获取参数并解压
assert len(sys.argv) == 2
p = Path(sys.argv[1])
assert p.is_file() and p.suffix.lower() == ".zip"
shutil.unpack_archive(p, p.parent)
print ("unzip ", p.resolve())

# 找到bin文件并移动到指定文件夹
targ = Path(r"C:\Users\32943\Desktop")
p = p.with_suffix("")
assert p.exists() == True and p.is_dir()
files = [f for f in p.rglob("*") if f.is_file() and f.suffix.lower() == ".bin"]
assert len(files) == 2
for f in files:
    shutil.copy2(f, targ)
    pf = Path( targ / f.name)
    print (f.absolute(), "copy to", pf.resolve())
    assert pf.exists()

# 读取xlsx获取ini文件
xlsx = r"C:\Users\32943\Desktop\a.xlsx"
df = pd.read_excel(xlsx)
base = Path(r"C:\Users\32943\Desktop\open")
ini_name = df["a"][0] + df["b"][0] + df["c"][0] + ".ini"


# 修改ini里面的值
for p in base.rglob(ini_name):
    if p.is_file() == False:
        continue
    shutil.copy2(p, targ / p.name)
    ini = Path(targ / p.name)
    config = configparser.ConfigParser()
    config.read(ini)
    
    # key
    print(config["DEFAULT"]["key"])
    config["DEFAULT"]["key"] = df["key"][0]
    print("key" , config["DEFAULT"]["key"])

    # key1
    for f in targ.rglob("*_key1*"):
        if f.is_file():
            config["DEFAULT"]["key1"] = f.name
    print("key1" ,  config["DEFAULT"]["key1"])
    # key2
    for f in targ.rglob("*_key2*"):
        if f.is_file():
            config["DEFAULT"]["key2"] = f.name
    config.write(ini)
    print("key2" ,  config["DEFAULT"]["key2"])

自动化解析脚本

import os, sys, shutil
from pathlib import Path
import subprocess as Cmd

assert len(sys.argv) == 2
src = sys.argv[1]
src = Path(src).resolve()
assert src.exists()

def func1(py : Path, dir : Path):
    shutil.copy2(py, dir)
    py = Path(dir / py.name)
    print("=====> Running : python ", py.name, "<=====")
    Cmd.run(["python", py], cwd = py.parent)
    py.unlink()

def func2(bin : Path, py: Path):
    # 执行命令
    shutil.copy2(bin, py.parent)
    print("=====> Running : python ", py.name, "<=====")
    Cmd.run(["python", py, py.parent / bin.name], cwd = py.parent)
    res = Path(py.parent / bin.stem)
    assert res.is_dir()

    # 移动
    shutil.move(res, bin.parent / bin.stem)
    res = Path(bin.parent / bin.stem)
    assert res.is_dir()

    # 移动到同目录下
    for f in res.rglob("merg_drv_*"):
        if f.is_file() and f.suffix.lower() == ".xlsx":
            shutil.move(f, bin.parent / f.name)
            assert Path(bin.parent / f.name).is_file()    

s1 = Path(r"TODO1").resolve()
s2 = Path(r"TODO2").resolve() # 修改脚本
s3 = Path(r"TODO3").resolve() # 修改脚本
assert s2.is_file() and s3.is_file()
if src.is_file() and src.suffix.lower() == ".bin":
    assert s1.is_file()
    func2(src, s2)
    func1(s3, src.parent)
elif src.is_dir():
    func1(s1, src)
    for f in src.rglob("*_*"):
        if f.is_file() and f.suffix.lower() == ".bin":
            func2(f, s2)
    func1(s3, src)

posted @ 2026-11-08 12:31  aswd  阅读(6)  评论(0)    收藏  举报