从另一个函数传递变量

时间:2019-01-12 16:17:07

标签: python python-3.x

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()

一切正常,直到调用deletegoogleapp函数并收到一个

  

\\ DESKTOP-62A8SSM \ C $ \ Users \“ 函数用户名位于   0x010C8B28 \ AppData \ Local \ Google

似乎没有将变量completepath从另一个函数传递到googleapp函数。

3 个答案:

答案 0 :(得分:0)

您需要如此存储username的返回值,现在,您什么也没有传递给deletegoogleapp。因此,您可以这样做:

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

在返回的路径u有效的情况下,此方法应该有效。

答案 1 :(得分:0)

更改您的函数用户名:

global completepath
global usernameinput

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

答案 2 :(得分:0)

修复了var的原始问题,并进行了其他一些编码更改

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()