来自python子进程的mysqldump单表 [英] mysqldump single table from python subprocess
问题描述
我正在尝试通过 mysqldump 从我在 Ubuntu 上运行的 python 脚本中转储单个表.
I'm trying to dump a single table via mysqldump from within my python script running on Ubuntu.
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: ">"
我尝试了几种不同的安排,但 mysqldump 总是需要另一个表名而不是表列表的末尾.
I've tried a couple different arrangements, but mysqldump always expects another table name rather than the end of the table list.
我需要做什么不同的事情?这是字符转义问题吗?
What do I need to do differently? Is this a character escaping issue?
推荐答案
>
不是命令参数,它是 shell 语法的一部分.由于您在调用 subprocess.run()
时没有使用 shell=True
,所以它不起作用.
>
is not a command argument, it's part of shell syntax. Since you're not using shell=True
when calling subprocess.run()
, it won't work.
您可以使用 subprocess.run()
的 stdout
参数来重定向到文件,而不是使用 shell 重定向.
Instead of using shell redirection, you can use the stdout
argument to subprocess.run()
to redirect to a file.
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)
这篇关于来自python子进程的mysqldump单表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!