0

I'm building a function to be made available via Pypi that allows building python files with a very nice header formatting. It already automates the creation date, the username, but I'm not able to capture the git user input. On linux just do ("git config --global user.name"). But in python the subprocesses I use always return me 0. Here's my code:

from art import *
from datetime import date
import getpass
import platform


def scriptHeaderPy(users=getpass.getuser(), fileName=input("What is the name of your project(s)?"), quat = int(input("How many files do you want to generate? "))):
        num = 0
        for num in range(quat):
            num=num+1
            path = f"{fileName}_{num}.py"
            with open(path, "w", encoding='UTF-8') as output:
                title = text2art(f"Hello,{users}!", font='fancy1',chr_ignore=False)
                file_name = f"filename: {fileName}_{num}.py"
                so = platform.system()
                system = f"system: {so}"
                ma = platform.architecture()
                bit = f"version: {ma[0]}"
                by = f"by: {users} <https://github.com/{users.lower()}>"
                data_atual = date.today()
                data = f"""created: {data_atual.strftime('%Y-%m-%d')}"""
                ast = '*'
                spac = ' '
                final = "import your librarys below"

                output.write(f"""

                    #{ast.ljust(80,ast)}#
                    #{spac.ljust(80,spac)}#
                    #{title.center(80)}#
                    #{spac.ljust(80,spac)}#
                    #   {file_name[:50].ljust(77)}#
                    #   {data.ljust(77)}#
                    #   {system.ljust(77)}#
                    #   {bit.ljust(77)}#
                    #   {by.rjust(76)} #
                    #{ast.ljust(80,ast)}#
                    #{final.center(80)}#
                    #{ast.ljust(80,ast)}#

                    """)
                print("files built successfully!")


  • Does this answer your question? [Running shell command and capturing the output](https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output) – mkrieger1 Mar 08 '22 at 16:05
  • Your code doesn't even start a subprocess. – mkrieger1 Mar 08 '22 at 16:06

1 Answers1

1

Connect the stdout of the subprocess to PIPE to get the output.

import subprocess

res = subprocess.run(["git", "config", "user.name"], stdout=subprocess.PIPE)
git_username = res.stdout.strip().decode()
kwsp
  • 1,184
  • 7
  • 26