为了账号安全,请及时绑定邮箱和手机立即绑定

线程函数后文件路径未保存

线程函数后文件路径未保存

慕哥9229398 2023-06-20 14:38:34
我正在使用线程搜索文件:import threadingdef 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 subprocesssubprocess.check_output(viber, shell=True)但我收到错误:NameError: name 'viber' is not defined我不知道该怎么做,以及如何解决它(((请有人帮忙!
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

当您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


查看完整回答
反对 回复 2023-06-20
  • 1 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信