0

I want to basically convert the bash shell script I have below into Python. Would anyone have an idea how I would be able to accomplish this please?

currentdir=`pwd`
find $currentdir -type f -exec grep -r 'host:' {} \; > GOODtest.txt

I almost have it in Python below: but I can't figure out how to get it to iterate over the list of files it found using glob. The code below only iterates over one file and stops

 import glob
 import re

 for name in glob.glob('*ssh_time'):
   strings=['host:']
   output=[]
   #print name
   myvar = name
   print myvar
   with open(myvar, 'rb+') as f:
     for line in f.readlines():
        for subs in strings:
            if subs in line:
                output.append(line)
 print(output)
MrE
  • 19,584
  • 12
  • 87
  • 105
Tbalz
  • 662
  • 1
  • 7
  • 24
  • first things first, is your glob returning anything? – MrE Jul 24 '17 at 00:33
  • I notice in one location you write `print myvar`, and at the bottom you write `print(output)`. The first call is python2 syntax, the second is python3. Also, do you realize that `glob.glob` is not recursive? – saintsfan342000 Jul 24 '17 at 00:45
  • 1
    Did you try os.walk? https://docs.python.org/3/library/os.html#os.walk or https://docs.python.org/2/library/os.html#os.walk – nit Jul 24 '17 at 09:40
  • Yes thanks tried it, but it didn't seem to help in this case – Tbalz Jul 24 '17 at 18:26
  • Have a look to my answer here: https://stackoverflow.com/a/45756760/8418376 -- it shows the basic principle of directory/file walking and processing. – Arminius Aug 18 '17 at 15:17

0 Answers0