最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Change value in Ini to empty using Python configparser - Stack Overflow

programmeradmin0浏览0评论

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)
processmunicate()

print(command)

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)
processmunicate()

print(command)
Share Improve this question edited Feb 2 at 22:16 ZCGCoder 5322 gold badges10 silver badges29 bronze badges asked Feb 1 at 23:49 Oussama GamerOussama Gamer 513 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can try using an if-else statement to check if there is a codesplit option passed from the config file or not.

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"

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

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

print(command)

This code checks if codesplit is there in the config. If so, it adds it to the command list. If not, it doesn't.

The output looks like

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

where the empty string is absent.

发布评论

评论列表(0)

  1. 暂无评论