Go through a file and save it in variables to make an FTP function?

0
login.txt
# Contenido del fichero login.txt
- ['10.0.0.3','Root','*****','/usr/amat','C:\Users\becario2adm','yyyymmdd']
- ['10.0.0.4','Administrador','*******','/zzz','C:\Users\becario2adm','yyyy_mm_dd']
- ['10.0.0.5','Administrador','*******','/','C:\Users\becario2adm','yyyy_mm_dd']

testFtp.py

import yaml
with open("login.txt") as f:
    data = yaml.safe_load(f)

for elemento in data:
    print(elemento[0], elemento[1],elemento[2], elemento[3],elemento[4], elemento[5])

from ftplib import FTP

funcion ftp()
ftp = FTP(dirServer)
ftp.login(user=usuario, passwd=passwd)

How can I do so that every line I read from login.txt takes the element 0 of the array and stores it in a dirServer variable, the element [1] stores it in a user variable, and the element [2]?

So on and use those variables to make as many Ftp connections as there are lines in login.txt

    
asked by antoniop 15.11.2018 в 11:31
source

1 answer

0

You have practically answered yourself:

  

How can I do so that every line I read from login.txt takes the element 0 of the array and stores it in a dirServer variable, the element [1] stores it in a user variable, and the element [2]?

Translating your Spanish to the python:

dirServer = elemento[0]
usuario = elemento[1]
passwd = elemento[2]

etc .. But this is the basic syntax of variable assignment, so I do not understand the doubt. Surely you are asking something else.

As for repeating connections for each element of the file, just enter the code that makes those connections within the same loop in which you print the values. Thus, in each iteration of the loop, it prints different values, and uses them as parameters for the corresponding connections. This is the idea:

from ftplib import FTP
import yaml

with open("login.txt") as f:
    data = yaml.safe_load(f)

for elemento in data:
    print(elemento[0], elemento[1],elemento[2], elemento[3],elemento[4], elemento[5])

    dirServer = elemento[0]   
    usuario = elemento[1]
    passwd = elemento[2]
    # etc...

    ftp = FTP(dirServer)
    ftp.login(user=usuario, passwd=passwd)
    # etc...

Anyway, I do not understand exactly where the question arises. It is a mere standard loop, which is repeated as many times as elements have the list data , that is, as many times as servers have you configured in the yaml file.

    
answered by 15.11.2018 / 11:59
source