配置文件中包含空格的python文件路径

xuo3flqw  于 2023-06-28  发布在  Python
关注(0)|答案(1)|浏览(210)

我有一个配置文件,我正在用ConfigParser阅读。其中一个值是路径中包含空格的文件路径:

[Configuration]
mysqldumpexecutable_location = C:/Program Files (x86)/MySQL/MySQL Server 5.7/bin/

似乎get()返回的这个参数的值只有“C:/Program”-直到空格。
下面是相关代码:

import os
import time
import datetime
import configparser

def BackupDB():

    try:
        config = configparser.RawConfigParser()
        config.read(r"etc\configuration.txt")
        DB_HOST = config.get('Configuration', 'db_host')
        DB_USER = config.get('Configuration', 'db_username')
        DB_USER_PASSWORD = config.get('Configuration', 'db_password')
        #DB_NAME = '/backup/dbnames.txt'
        DB_NAME = config.get('Configuration', 'db_schema')
        BACKUP_PATH = config.get('Configuration', 'backup_path')
        MYSQLDUMP_LOCATION = config.get('Configuration', 'mysqldumpexecutable_location')

        DATETIME = time.strftime('%d%m%Y-%H%M%S')
        TODAYBACKUPPATH = BACKUP_PATH + DATETIME
        db = DB_NAME

        dumpcmd = MYSQLDUMP_LOCATION + "mysqldump -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + TODAYBACKUPPATH + "/" + db + ".sql"
        os.system(dumpcmd)
    except Exception as e:
        print("something here")
        return None

我得到这个错误:* 'c:/Program'无法识别为内部或外部命令、可操作程序或批处理文件。*
如何正确传递此Windows路径?
谢谢!

u91tlkcl

u91tlkcl1#

我意识到这篇文章最初发表已经五年了,但是你应该用双引号括起任何带有空格的路径。

[Configuration]
mysqldumpexecutable_location = "C:/Program Files (x86)/MySQL/MySQL Server 5.7/bin/"

相关问题