Reuse function variables

Asked

Viewed 157 times

0

I have a script in Python divided into functions and need to access a function variable in another example function:

import glob, os, time, datetime, sys, platform
from datetime import datetime
from datetime import timedelta

os.chdir = 'C:\Zabbix\Temp'

#Pegar SO e retorna os valores de acordo com a plataforma: 0 - Linux; 1 - Windows; 2 - ERROR
def getOperationSystem():
    OpSys = platform.system()
    print(OpSys)
    if OpSys == 'Windows':
        return(getParameters('C:\\Zabbix\\Parameters.txt'))
    elif OpSys == 'Linux':
        return('#')
    else:
        print('Sistema Operacional desconhecido')
        return(2)
        StopIteration()

#Pegar os parametros no arquivo Parameters
def getParameters(dirBkp):
    with open(dirBkp, "r") as arq:
        for linha in arq:
            global values
            values = linha.split(';')
            return getTasks(values[2])

#Valida se o processo do backup está em execução: 0 - Sim; 1 - Não
def getTasks(name):
    read_process = os.popen('tasklist /v').read().strip().split('\n')
    for i in range(len(read_process)):
        process = read_process[i]
        if name in read_process[i]:
            return 0
            StopIteration    
    getTimeArq(values)

#Valor tempo do ultimo arquivo
def getTimeArq(values):

    bkp_name = values[2]
    print("Nome do Bkp:\t\t\t", bkp_name)
    bkp_name = bkp_name+"*.log"
    print(bkp_name)

    log = glob.glob(bkp_name)
    log.sort(key=os.path.getctime, reverse=True)  

    print (log[0])

In the case, the getParameters, has a variable that should be used in 3 or more functions of 20 (which is in the script)

It loads several values where each function pulls a value, while trying to use it in the getTimeArq he returned this to me:

inserir a descrição da imagem aqui

  • 1

    By the message, you tried to call the function getTimeArq without specifying the parameters. To call a function, you need to place the parentheses and, in this case, the value of values. Something like x = getTimeArq(meus_values).

  • Even using the method you recommended, now he gives me the None return... as if it were empty =/

  • You could post the 'error' or output completely ?

  • 1

    Of course, to make it easier I’ll give an Edit posted all the code and the print of the output OK?

  • I have another script that works (because it was designed to receive the parameters coming from the command line and it is lake that can no longer occur so I put all the parameters inside a txt that the value variable is receiving and printing the values as needed but when passing to the Variable LOG it is void

  • Cara I solved hsuahsuhauhsauhsaus Vlw by helping I who did shit and had not seen I will edit with the answer

  • In that case, answer your own post with your answer. In the most elaborate way possible. So whoever finds the question somehow might have an idea of what you did to solve

  • 1

    Dear Wallace, use the answer field to add the answer. Take the tour to understand how the site works https://answall.com/tour

Show 3 more comments

1 answer

0

Create an object outside the function and pass it as argument.

Ex.:

#!/bin/python3
class fake_global_variables(object):
    nome = ''

def get_dados(memory):
    memory.nome = input('Nome :')
    return ('Que nome legal.')

fake = fake_global_variables()
get_dados(fake)
print(fake.nome)

Browser other questions tagged

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