首页 > 解决方案 > 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.

标签: pythonpython-3.x

解决方案


您需要存储 的返回值username,现在,您没有将任何内容传递给deletegoogleapp. 所以你可以这样做:

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

鉴于返回的路径u有效,这应该可以工作。


推荐阅读