Search code examples
pythonbatch-filefor-looppyuicpyrcc

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


I want to create a short script that loops over all .ui and .qrc files in a folder, and calls pyuic4 and pyrcc4 respectively for each file thus generating the corresponding .py files.

EDIT months later after i learned more 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.')

Solution

  • 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.')