Home > Software design >  Insert variable inside a check_call / check_output in python 2.7?
Insert variable inside a check_call / check_output in python 2.7?

Time:12-16

Good morning everyone,

I'm trying to make a mini software where i read a csv, insert it into a variable and then give this variable to a check_call function. The CSV is a list of databases:

cat test_db.csv

andreadb
billing
fabiodb

And this is what i wrote right now:

from subprocess import *
import csv

#Load the CSV inside the variable data
with open('test_db.csv', 'r') as csvfile:
     data = list(csv.reader(csvfile))
#For loop that per each database it shows me the tables and the output saved into risultato.txt
for line in data:
        database = line
        check_call[("beeline", "-e", "\"SHOW TABLES FROM \"", database, ";" , ">>" , "risultato.txt")]

When i execute it i get the following error:

Traceback (most recent call last):
  File "test_query.py", line 10, in <module>
    check_call[("beeline", "-e", "\"SHOW TABLES FROM \"", database, ";")]
TypeError: 'function' object has no attribute '__getitem__'

I'm relatively new to python and this is my first project, so any help would be great. If i didn't explained correctly something, please tell me and i'll edit the post. Thanks!!

CodePudding user response:

You have mistyped the function call. It should be

check_call(["beeline", "-e", "\"SHOW TABLES FROM \"", database, ";" , ">>" , "risultato.txt"])

The ( was placed after [ in your question. It should be ( first followed by a list of commands and params.

CodePudding user response:

Thanks for the reply, after i changed as you suggested i get a different error:

Traceback (most recent call last):
  File "test_query.py", line 10, in <module>
    check_call(["beeline", "-e", "\"SHOW TABLES FROM \"", database, ";"])
  File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib64/python2.7/subprocess.py", line 524, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
TypeError: execv() arg 2 must contain only strings

But from a quick research online it meas that i can't use the var but only string

  • Related