Search substring in all text files in a directorysteemCreated with Sketch.

in Crypto Talk4 years ago

The following code searches a specific substring in all 'txt' file in a directory.

import os
import time
directory = 'D:/Python/file1/'

for filename in os.listdir(directory):
    filesize = os.stat(filename).st_size
    if (filesize>60000):
        continue
    # print(filename)
    
    with open(filename, 'rb') as f:
        lines = f.readlines()
        for line in lines:
            tempstr = str(line)
            le = len(tempstr)
            #print(tempstr)
            findresults=tempstr.find("binance") 
            #print(findresults)
            # time.sleep(5)
            if (findresults!=-1):
                print(filename)
                print(tempstr)

Coin Marketplace

STEEM 0.28
TRX 0.11
JST 0.031
BTC 67962.69
ETH 3852.35
USDT 1.00
SBD 3.66