如何解决穿线功能后未保存文件路径
我正在使用Threading搜索文件
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。