1 回答

TA貢獻1784條經(jīng)驗 獲得超8個贊
當您viber在函數(shù)中聲明變量時,python 認為該變量是本地變量,并會在函數(shù)結(jié)束時將其刪除。
您只需要聲明viber為全局變量,這樣函數(shù)就不會聲明它自己的變量。
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
添加回答
舉報