繁体   English   中英

从另一个函数传递变量

[英]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()

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

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

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

您需要这样存储username的返回值,现在,您什么也没有传递给deletegoogleapp 因此,您可以执行以下操作:

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

这应该给出的路径u返回是有效的。

更改您的函数用户名:

global completepath
global usernameinput

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

修复了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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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