for循环调用pyuic4生成所有py文件 |从 python 调用 .bat

Calling pyuic4 in a for loop to generate all py files | Calling .bat from python

我想创建一个循环遍历文件夹中所有 .ui 和 .qrc 文件的短脚本,并分别为每个文件调用 pyuic4 和 pyrcc4,从而生成相应的 . py 文件。

几个月后在我学到更多后编辑 python:

import shutil
import os


# pyrcc5 pixels.qrc -o pixels_rc.py && pyuic5 mainWindowGUI.ui -o MainWindowGUI.py && pyuic5 titleBarGUI.ui -o TitleBarGUI.py

command = 'pyrcc5 pixels.qrc -o pixels_rc.py'

for file in os.listdir(os.getcwd()):
    if file.endswith('ui'):
        filename = file.split('.')[0]
        command = f'{command} && pyuic5 {filename}.ui -o {filename}.py'

print(f'\n Executing: {command} \n')

try:
    result = os.system(command)
    if result is 0:
        pass
except Exception as e:
    print('Error {e}')

print('\n Done.')
from pathlib import Path
import os

# generate something like this and the run it
# @echo off
# pyrcc5 pixels.qrc -o pixels_rc.py && pyuic5 window.ui -o window.py && pyuic5 dialog.ui -o dialog.py

command = f'pyrcc.exe Design/pixels.qrc -o GeneratedFiles/pixels_rc.py'

for file in os.listdir(Path(os.getcwd()+'/Design')):
    if file.endswith('ui'):
        filename = file.split('.')[0]
        command = command + f" && pyrcc.exe Design/{filename}.ui -o GeneratedFiles/{filename}.py"

try:
    result = os.system(command)
    if result == 0:
        pass
except Exception as e:
    print('Error {e}')

print('\n Done.')