Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I';I’我在试着理解如何从同一个类中的另一个函数调用一个函数_Python_Cmd_Function Calls - Fatal编程技术网

Python I';I’我在试着理解如何从同一个类中的另一个函数调用一个函数

Python I';I’我在试着理解如何从同一个类中的另一个函数调用一个函数,python,cmd,function-calls,Python,Cmd,Function Calls,我正在尝试登录到Ubuntu服务器,并使用一个已经在本地运行的函数(Python2.7-win7机器)在几个不同的路径上搜索日志。下面是我如何登录和选择日志的函数(我的程序的基础是Python的cmd模块): 下面是我要调用的函数(在同一个类中): def do_search(self, line): print '\nCurrent dir: '+ os.getcwd() userpath = raw_input("\nPlease enter a path to sear

我正在尝试登录到Ubuntu服务器,并使用一个已经在本地运行的函数(Python2.7-win7机器)在几个不同的路径上搜索日志。下面是我如何登录和选择日志的函数(我的程序的基础是Python的cmd模块):

下面是我要调用的函数(在同一个类中):

def do_search(self, line):
    print '\nCurrent dir: '+ os.getcwd()  
    userpath = raw_input("\nPlease enter a path to search (only enter folder   name,   eg. SQA\log): ")  
    directory = os.path.join("c:\\",userpath)
    os.chdir(directory)
    print "\n                               SEARCHES ARE CASE SENSITIVE"
    print " "
    line = "[1]Single File [2]Multiple Files [3]STATIC HEX"
    col1 = line[0:14]
    col2 = line[15:32]
    col3 = line[33:46]
    print "                   " + col1 + "     " + col2 + "     " + col3
    print "\nCurrent Dir: " + os.getcwd()
    searchType = raw_input("\nSelect type of search: ")
       if searchType == '1':  
          logfile = raw_input("\nEnter filename to search (eg. name.log): ")   
          fiLe = open(logfile, "r")
          userString = raw_input("\nEnter a string name to search: ")
          for i,line in enumerate(fiLe.readlines()):
              if userString in line:
                 print "String: " + userString
                 print "File: " + os.join(directory,logfile)
                 print "Line: " + str(i)
                 break
          else:
              print "%s NOT in %s" % (userString, logfile)

          fiLe.close()
       elif searchType =='2':
          print "\nDirectory to be searched: " + directory
          #directory = os.path.join("c:\\","SQA_log")
          userstring = raw_input("Enter a string name to search: ")
          userStrHEX = userstring.encode('hex')
          userStrASCII = ''.join(str(ord(char)) for char in userstring)
          regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII )))
          choice = raw_input("1: search with respect to whitespace. 2: search ignoring whitespace: ")
          if choice == '1':
               for root,dirname, files in os.walk(directory):
                  for file in files:
                      if file.endswith(".log") or file.endswith(".txt"):
                         f=open(os.path.join(root, file))
                         for i,line in enumerate(f.readlines()):
                             result = regex.search(line)
                             if result:
                                print "\nLine: " + str(i)
                                print "File: " + os.path.join(root,file)
                                print "String Type: " + result.group() + '\n'



                         f.close()
          re.purge()              
          if choice == '2':
             regex2  = re.compile(r'\s+')
             for root,dirname, files in os.walk(directory):
                 for file in files:
                     if file.endswith(".log") or file.endswith(".txt"):
                        f=open(os.path.join(root, file))
                        for i,line in enumerate(f.readlines()):
                            result2 = regex.search(re.sub(regex2, '',line))
                            if result2:
                               print "\nLine: " + str(i)
                               print "File: " + os.path.join(root,file)
                               print "String Type: " + result2.group() + '\n'


                        f.close()  


                        re.purge()

       elif searchType =='3':
          print "\nDirectory to be searched: " + directory
          print " "
          #directory = os.path.join("c:\\","SQA_log")
          regex = re.compile(r'(?:3\d){6}')
          for root,dirname, files in os.walk(directory):
             for file in files:
               if file.endswith(".log") or file.endswith(".txt"):
                  f=open(os.path.join(root,file))
                  for i, line in enumerate(f.readlines()):
                      searchedstr = regex.findall(line)
                      ln = str(i)
                      for word in searchedstr:
                         print "\nString found: " + word
                         print "Line: " + ln
                         print "File: " + os.path.join(root,file)
                         print " "
                         logfile = open('result3.log', 'w')

                  f.close()

          re.purge()

方法是通过拥有它们的对象调用的

self.do_search(...)
仅此而已

self.do_search(...)
self.do_search(linenumber)