3

I have to launch a python sql file. The file is for mysql. I tried it like this:

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()

But I get this error:

ERROR: must be str, not _io.TextIOWrapper

how can I do?

asked May 9, 2017 at 8:34
1
  • This one looks like what you want. stackoverflow.com/a/4563950/1394353 note what the source is using - the file name, not contents. So dont open the file Commented May 9, 2017 at 15:07

2 Answers 2

2

You need to pass the contents of the file, not the file object.

proc.communicate('source ' + fileInput.read())

Also, please don't catch exceptions just to print them and exit. That's what Python does already. Leave out that try/except.

answered May 9, 2017 at 8:40
Sign up to request clarification or add additional context in comments.

Comments

0

Ok, I moved the mysql instructions inside a bat.

from subprocess import Popen
class ImportSql:
 def execImport(self):
 p = Popen("import.bat")
 p.communicate()

it's ok! thanks!

answered May 10, 2017 at 10:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.