Return of a for in a python list

Asked

Viewed 99 times

0

I would like to use the return of a for that list files from a directory to use in a Mysql command, but I can’t get all the files. thanks in advance for the help!

#Lista os arquivos do diretorio somente os arquivos de log
for i in glob.glob("C:\Backups\Logs\*.0000*"): 
     lista = [i]  

#Busca no banco de dados o ultimo Id inserido
cursor = conn.cursor()
cursor.execute('Select * from logbin where Id = (select max(Id) from logbin)')
recordset = cursor.fetchall()

for record in recordset:
    print (record[1],"-->", record[2])

#Improta comandos do cmd - pip install plumbum.
from plumbum.cmd import mysqlbinlog
print  ( mysqlbinlog [lista, '-rC:\\Backups\\Logs\\Binlog.txt']())

1 answer

1


In your code, you’re recreating lista at each iteration. Put a print(lista), as below, and you will see what is happening.

for i in glob.glob("C:\Backups\Logs\*.0000*"): 
     lista = [i]  
     print(lista)

The way out will be something like:

['C:\Backups\Logs\asdf.0000asdf']
['C:\Backups\Logs\qwer.0000qwer']
['C:\Backups\Logs\aaar.000023232']

That is, is creating 3 lists in this case, a list for each existing file in the log directory.

To generate a single list of all files returned by glob, can do so:

lista = glob.glob('C:\Backups\Logs\*.0000*')
print (lista)

The output is a single list:

['C:\Backups\Logs\asdf.0000asdf','C:\Backups\Logs\qwer.0000qwer','C:\Backups\Logs\aaar.000023232']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.