简体   繁体   English

mysqldump 来自 python 子进程的单表

[英]mysqldump single table from python subprocess

I'm trying to dump a single table via mysqldump from within my python script running on Ubuntu.我正在尝试通过 mysqldump 从在 Ubuntu 上运行的 python 脚本中转储单个表。

args = ["mysqldump", f"-u{DBUser}", f"-h{DBHost}", f"-p{DBPass}", "--no-tablespaces", f'{DatabaseName}', f'{ArchiveTable}', '>', f'{BucketReadyName}']


subproc_output = subprocess.run(args)

This code gives this error.这段代码给出了这个错误。

mysqldump: Couldn't find table: ">"

I've tried a couple different arrangements, but mysqldump always expects another table name rather than the end of the table list.我尝试了几种不同的安排,但 mysqldump 总是期望另一个表名而不是表列表的末尾。

What do I need to do differently?我需要做些什么不同的事情? Is this a character escaping issue?这是字符 escaping 问题吗?

> is not a command argument, it's part of shell syntax. >不是命令参数,它是 shell 语法的一部分。 Since you're not using shell=True when calling subprocess.run() , it won't work.由于您在调用subprocess.run()时没有使用shell=True ,因此它将不起作用。

Instead of using shell redirection, you can use the stdout argument to subprocess.run() to redirect to a file.您可以使用 subprocess.run subprocess.run()stdout参数来重定向到文件,而不是使用 shell 重定向。

args = ["mysqldump", f"-u{DBUser}", f"-h{DBHost}", f"-p{DBPass}", "--no-tablespaces", f'{DatabaseName}', f'{ArchiveTable}']

with open(BucketReadyName, 'w') as outfile:
    subprocess.run(args, stdout=outfile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM