python - Find fileS and then find a string in those files -


i have written function finds of version.php files in path. trying take output of function , find line file. function finds files is:

def find_file():   root, folders, files in os.walk(acctpath):     file in files:       if file == 'version.php':         print os.path.join(root,file) find_file() 

there several version.php files in path , return string each of files.

edit: thank suggestions, implementation of code didn't fit need. able figure out creating list , passing each item second part. may not best way it, i've been doing python few days.

def cmsoutput():   filelist = []   root, folders, files in os.walk(acctpath):     file in files:       if file == 'version.php':         filelist.append(os.path.join(root,file))    path in filelist:     open(path) f:       line in f:         if line.startswith("$wp_version ="):           version_number = line[15:20]           inst_path = re.sub('wp-includes/version.php', '', path)           version_number = re.sub('\';', '', version_number)           print inst_path + " = " + version_number  cmsoutput() 

since want use output of function, have return something. printing not cut it. assuming works has modified follows:

import os   def find_file():     root, folders, files in os.walk(acctpath):         file in files:             if file == 'version.php':                 return os.path.join(root,file)  foundfile = find_file() 

now variable foundfile contains path of file want at. looking string in file can done so:

with open(foundfile, 'r') f:     content = f.readlines()     lines in content:         if '$wp_version =' in lines:             print(lines) 

or in function version:

def find_in_file(string_to_find, file_to_search):     open(file_to_search, 'r') f:         content = f.readlines()         lines in content:             if string_to_find in lines:                 return lines  # can call this: find_in_file("$wp_version =", find_file()) 

note function version of code above terminate finds 1 instance of string looking for. if wanna them all, has modified.


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -