1 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
當(dāng)您viber在函數(shù)中聲明變量時(shí),python 認(rèn)為該變量是本地變量,并會(huì)在函數(shù)結(jié)束時(shí)將其刪除。
您只需要聲明viber為全局變量,這樣函數(shù)就不會(huì)聲明它自己的變量。
viber = None # declare global variable # add this line
import threading
def thread(seconds):
global viber # use global variable # add this line
for root, dirs, files in os.walk('/'):
for file in files:
if file == 'Viber.exe':
viber = os.path.join(root, file)
print(viber)
print("Finish")
threading.Thread(target = thread, args = (1,), daemon = True).start()
###########
import subprocess
subprocess.check_output(viber, shell=True) # use global variable
添加回答
舉報(bào)