简体   繁体   中英

Passing a variable from another function

import os
import shutil


def listdirectory():
    global computername
    computername = input("What is the computer name? ")
    completepathlist = fr"\\{computername}\C$\Users"
    return os.listdir(completepathlist)

def username():
    global completepath
    global usernameinput
    usernameinput = input("What is the user name? ")
    completepath = fr"\\{computername}\C$\Users\{username}\AppData\Local\Google"

def programrunningcheck():
    password = input("What is your password? ")
    command = "taskkill /s " + str(computername) + " /u " + str(usernameinput) + " /p " +password+ " /im chrome.exe"
    print(command)
    os.system(command)

def deletegoogleapp():
    shutil.rmtree(completepath)

#Functions being called
print(listdirectory())
username()
programrunningcheck()
deletegoogleapp()

Everything works up until deletegoogleapp function is called and receive a

\\\\DESKTOP-62A8SSM\\C$\\Users\\" function username at 0x010C8B28 \\AppData\\Local\\Google

looks to be not passing the variable completepath from another function to the googleapp function.

You need so store the return value of username , right now, you are passing nothing to deletegoogleapp . So you can do:

u <- username()
programrunningcheck()
deletegoogleapp(u)

This should work given the path u returned is valid.

change you'r function username:

global completepath
global usernameinput

def username(self):
    usernameinput = input("What is the user name? ")
    completepath = fr"\\{computername}\C$\Users\{username}\AppData\Local\Google"

Fixed original issue with var and made some other coding change

import os
import shutil
import time


def listdirectory():
    global computername
    computername = input("What is the computer name? ")
    completepathlist = fr"\\{computername}\C$\Users"
    return os.listdir(completepathlist)

def username():
    global completepath
    usernameinput = input("What is the user name? ")
    completepath = fr"\\{computername}\C$\Users\{usernameinput}\AppData\Local\Google"

def programrunningcheck():
    print("We need your credentials to kill chrome")
    techuser = input("What is your username? ")
    techpassword = input("What is your password? ")
    command = "taskkill /s " + str(computername) + " /u " + str(techuser) + " /p " +(techpassword)+ " /im chrome.exe"
    time.sleep(5)
    print(command)
    os.system(command)

def deletegoogleapp():
    shutil.rmtree(completepath)

#Functions being called
print(listdirectory())
username()
programrunningcheck()
deletegoogleapp()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM