[英]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.