简体   繁体   中英

mysqldump single table from python subprocess

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: ">"

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?

> 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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