Python – alternative for multiple os.system command

Below are the shell commands, that does few variable and file manipulation and uploads the final file to a repository.

Using repetitive os.system() is one option. Is there a simpler and elegant way to address this ?

Even with the os.system(), I’m facing issues with quotes and the variable replacement is throwing error.

I don’t want to club these commands as a script and execute it 🙂 Any input would be really helpful, thanks.

target_folder = tmp_folder+'/tmp'+str(count)
FILE_TYPE = "bundle"
FILE_NAME = "solution"

    set +x
    file_name_wo_ext="$(cat 'target_folder'/*.yaml| grep -i name | awk '{print $2}' | awk '{print $1}')"
    tar_file_name=$(ls $output_folder | grep tar.gz)
    mv $file_path "`basename $file_path .tar.gz`.tgz"
    wget command to upload the file


ls: cannot access ‘/home/+FILE_NAME/+FILE_TYPE+/cat’: No such file or directory

ls: cannot access ‘+target_folder/*.yaml|’: No such file or directory

OS: Ubuntu 18.04.


An approximation of your shell script in Python might be something like this.

Do note that your original script will behave in undefined ways if there are e.g. multiple name lines in the YAML, or multiple .tar.gzes…

import glob
import os

def find_filename_wo_ext(target_folder):
    for yaml_filename in glob.glob(os.path.join(target_folder, "*.yaml")):
        with open(yaml_filename, "r") as yaml_file:
            for line in yaml_file:
                if "name" in line.lower():
                    return line.partition(" ")[-1]  # might be wrong :)

def main():
    tmp_folder = "/tmp"
    count = 1

    target_folder = os.path.join(tmp_folder, f"tmp{count}")
    FILE_TYPE = "bundle"
    FILE_NAME = "solution"

    file_name_wo_ext = find_filename_wo_ext(target_folder)
    output_folder = os.path.join("/home", FILE_TYPE, FILE_NAME, file_name_wo_ext)
    tar_file_name = list(
        glob.glob(os.path.join(output_folder, "*.tar.gz"))
    file_path = os.path.join(output_folder, tar_file_name)
    os.rename(file_path, os.path.splitext(file_path)[0] + ".tgz")