Python Recursive search through the rows and files?

Good day!
I have two functions:

This function takes the file and puts it into line form.

def getstrings(content):
 file = open(content)
 return file.readlines()

 oneopenfile = getstrings(content)


This function takes the variable data and cyclically generates row-based search data and writes the result to another file.
def search_data(oneopenfile):
 result = open((fileresult + "." + filetype), 'w')
 for i in oneopenfile:
 if i.count(searchinfo):
result.write(i)

 search_data(oneopenfile)


How to implement that would be the same was not done for a particular file and on all files in a directory?

Solved: https://github.com/onixdclxvi/PyDCLXVI/blob/master...
June 26th 19 at 13:58
3 answers
June 26th 19 at 14:00
Solution
for d, dirs, files in os.walk('/home/user/dir'):
 for f in files:
 getstrings(f)


or
for f in os.listdir('/home/path/dir'):
 getstrings(f)
June 26th 19 at 14:02
Solution


so here is what came up, but not working)

Collect the names of the files with a txt extension in the file line by line and write each name in the file.
def filebasedir():
 filetodir = os.walk('.')
 tempfile = open('tempfilebase.txt', 'w')
 for line, dirs, files in filetodir:
 for f in files:
 if f.count(".txt"):
tempfile.write(f+"\n")

filebasedir()


Then try to search around all the files and all the lines in them.

def search_all_data():
 result = open(("TESTRESULT.txt"), 'w')
 allopenfile = open('tempfilebase.txt', 'r')
 for linenamefile in allopenfile:
 linenamefile = open(linenamefile)
 print (linenamefile)
 dataonefile = linenamefile #.readlines()
 for data in dataonefile:
 if data.count("root"):
result.write(data)

search_all_data()


In the end I receive an error

Traceback (most recent call last):
 File "test.py", line 34, in <module>
search_all_data()
 File "test.py", line 27, in search_all_data
 linenamefile = open(linenamefile)
FileNotFoundError: [Errno 2] No such file or directory: '111.txt\n'</module>


I understand he can't find the file 111.txt\n due to the fact that right indent \n?
in General, as the case correct? could you tell me? only the second day learning Python
for d, dirs, files in os.walk('.'):
 for f in files:
 if f.endswith('.txt'):
 getstrings(f)

And not to build any bikes - tate_Stracke78 commented on June 26th 19 at 14:05
:
Dmitry, would you be so kind to scatter for Amateur, how to sample for all lines and all TXT files in the directory, twist twirl don't understand - coby_Brekke28 commented on June 26th 19 at 14:08
The file you read in getstrings and get files using code that I wrote - tate_Stracke78 commented on June 26th 19 at 14:11
:

That's what happened,

import os

def searchdata(f):
 zapis = ('TESTRESULT.txt')
 result = open(zapis, 'w')
 files = open(f, 'r')
 for i in files:
 if i.count('ftp'):
 print (i)
result.write(i)

for d, dirs, files in os.walk('.'):
 for f in files:
 if f.endswith('.txt'):
 searchdata(f)


but for some reason the file does not record, runs, and judging by the print very small, as if not all files run or all parses - coby_Brekke28 commented on June 26th 19 at 14:14
: but no! it works! thank you! - coby_Brekke28 commented on June 26th 19 at 14:17
June 26th 19 at 14:04
To read documentation on standard library functions os.walk()

Find more questions by tags Python