python路径字符串作为参数传递,传参个数报错

Python/PHP/Perl 开发与设计
回复
liku
帖子: 4
注册时间: 2016-12-11 20:30
系统: ubuntu
送出感谢: 1 次
接收感谢: 0

python路径字符串作为参数传递,传参个数报错

#1

帖子 liku » 2016-12-16 17:41

做一个linux的文件检索,路径字符串传参报错

代码: 全选

class fileFind:

    catalog = ""
    key = ""
    result = []

    def __init__(self):
        self.catalog = "/"

    def startDir(self,dir):
        self.catalog = dir
        return self.catalog

    def setKey(self,key):
        self.key = key

    def fileSearch(self,path):
        for root,dir,file in os.walk(path):
            for name in dir:
                self.searchThread(os.path.join(path,name))
            break
        for f in os.listdir(path):
            if (f.strip().find(self.key)>-1):
                self.result.append(os.path.join(path,f.strip()))
                print(os.path.join(path,f.strip()))    #查找结果get

    def searchThread(self,path):
        t = threading.Thread(target=self.fileSearch,args=path)
        t.setDaemon(True)
        t.start()
        t.join()

if __name__=="__main__":
    a = fileFind()
    a.startDir("/home/liku")
    a.setKey("")
    a.searchThread(a.catalog)
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
TypeError: fileSearch() takes 2 positional arguments but 16 were given
头像
oneleaf
论坛管理员
帖子: 10237
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04
送出感谢: 7 次
接收感谢: 107 次

Re: python路径字符串作为参数传递,传参个数报错

#2

帖子 oneleaf » 2016-12-16 17:59

代码: 全选

t = threading.Thread(target=self.fileSearch,args=path)
试试:

代码: 全选

t = threading.Thread(target=self.fileSearch,args=(path,))
这些用户感谢了作者 oneleaf 于这个帖子:
liku (2016-12-16 21:04)
评价: 3.7%
liku
帖子: 4
注册时间: 2016-12-11 20:30
系统: ubuntu
送出感谢: 1 次
接收感谢: 0

Re: python路径字符串作为参数传递,传参个数报错

#3

帖子 liku » 2016-12-16 18:25

oneleaf 写了:

代码: 全选

t = threading.Thread(target=self.fileSearch,args=path)
试试:

代码: 全选

t = threading.Thread(target=self.fileSearch,args=(path,))

棒!!!
什么原理....
回复

回到 “Python/Php/Perl”