Python 线程功能后文件路径未保存

Python 线程功能后文件路径未保存,python,variables,subprocess,python-multithreading,Python,Variables,Subprocess,Python Multithreading,我正在搜索带有线程的文件: import threading def thread(seconds): for root, dirs, files in os.walk('/'): for file in files: if file == 'Viber.exe': viber = os.path.join(root, file) print(viber) print(&qu

我正在搜索带有线程的文件:

import threading
def thread(seconds):
    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)
但我得到了一个错误:

NameError: name 'viber' is not defined

我不知道该怎么做,以及如何修复它(((请有人帮助!

当您在函数中声明
viber
变量时,python认为该变量是本地变量,并将在函数结束时删除它)

您只需要将
viber
声明为全局变量,这样函数就不会声明自己的变量

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

全是最好的!