[Python] change value in ini to empty Using Python configparser

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I want to read the config file if the value is empty

    example : To convert a video file to audio file

    config.ini
    [settings]
    videofile = video.avi
    codesplit = -vn
    outputfile = audio.mp3


    Output

    ['ffmpeg.exe', '-i', 'video.avi', '-vn', 'audio3.mp3']


    If you make the value "codesplit" empty, the code will not work. example : To convert a video avi file to video mp4

    [settings]
    videofile = video.avi
    codesplit =
    outputfile = videoaudio.mp4


    Output

    ['ffmpeg.exe', '-i', 'video.avi', '', 'videoaudio.mp4']


    I want to remove this quote so that the code to works

    '',


    full code

    import subprocess
    import configparser


    config = configparser.ConfigParser(allow_no_value=True)
    config.read(r'config.ini')
    videofile = config.get('settings', 'videofile')
    outputfile = config.get('settings', 'outputfile')
    codesplit = config.get('settings', 'codesplit', fallback=None)



    ffmpeg_path = r"ffmpeg.exe"


    command = [
    f"{ffmpeg_path}",
    "-i", (videofile),
    (codesplit),
    (outputfile),]

    process = subprocess.Popen(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True,
    bufsize=1,
    universal_newlines=True)
    process.communicate()

    print(command)

    Continue reading...

Compartilhe esta Página