从另一个函数传递变量

2024-06-26 14:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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\"function username at 0x010C8B28\AppData\Local\Google

看起来没有将变量completepath从另一个函数传递到googleapp函数。你知道吗


Tags: 函数inputisosdefusernamepasswordglobal
3条回答

更改您的函数用户名:

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

您需要存储username的返回值,现在,您没有向deletegoogleapp传递任何内容。所以你可以:

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

如果返回的路径u是有效的,则应该可以这样做。你知道吗

相关问题 更多 >