使用Pythonconfigparser

问题描述 投票:0回答:1

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

输出

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

如果您将值“代码”为空,则代码将不起作用。 示例:将视频AVI文件转换为视频MP4

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

输出

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

我想删除此报价,以便代码可行

 '',

Full代码

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)

您可以尝试使用IF-ELSE语句检查是否有从配置文件传递的
codesplit
python configparser
1个回答
0
投票
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, ) process.communicate() print(command)

该代码检查是否在配置中是否存在。如果是这样,它将其添加到
codesplit

列表中。如果没有,则不会。
输出看起来像

command

没有空字符串的地方。
    
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.