【发布时间】:2017-10-07 11:27:33
【问题描述】:
我必须启动一个 python sql 文件。 该文件用于mysql。 我试过这样:
from subprocess import Popen, PIPE
import sys
class ImportSql:
def execImport(self, fileSql):
try:
with open(fileSql, 'r') as fileInput:
proc = Popen(["mysql", "DB_NAME", "-u", "USER", "-pPASSWORD"], stdin=PIPE, stdout=PIPE)
proc.communicate('source ' + fileInput)[0]
except BaseException as ex:
print("ERROR:", ex)
sys.exit()
但我收到此错误:
错误:必须是 str,而不是 _io.TextIOWrapper
我该怎么办?
【问题讨论】:
-
这个看起来像你想要的。 stackoverflow.com/a/4563950/1394353 注意源使用的是什么 - 文件名,而不是内容。所以不要打开文件
标签: python mysql sql mariadb popen