Python MySQL 使用输出重定向 (<) 管道转储导入的 Sh 库

Python Sh Library for MySQL dump import using output redirection (<) pipes

我正在使用 Python Sh (https://amoffat.github.io/sh/),我想知道是否也可以使用 <> 管道?

主要是说mysql < dump.sql。 我正在考虑使用 mysql -D db -e "source mydump.sql',但不推荐使用 source

还有其他选择吗?

您可以参考文档的 STDIN 处理 (http://amoffat.github.io/sh/#stdin-processing) and Redirection (http://amoffat.github.io/sh/#redirection) 部分。也就是说,您的代码:

mysql < dump.sql

应该等同于:

dump_file = open("home/user/dump.sql", "r")
sh.mysql(_in=dump_file)
dump_file.close()