xxxxxxxxxx
with open('largeFile', 'r') as inF:
for line in inF:
if 'myString' in line:
# do_something
xxxxxxxxxx
import os
import re
import win32api
def find_file(root_folder, rex):
for root,dirs,files in os.walk(root_folder):
for f in files:
result = rex.search(f)
if result:
print os.path.join(root, f)
break # if you want to find only one
def find_file_in_all_drives(file_name):
#create a regular expression for the file
rex = re.compile(file_name)
for drive in win32api.GetLogicalDriveStrings().split('\000')[:-1]:
find_file( drive, rex )
find_file_in_all_drives( 'myfile\.doc' )
xxxxxxxxxx
# Open the file
fhand = open('path and the name of the file')
count = 0
# iterate through the lines in the file
for line in fhand:
# Remove the \n at the end and stop printing an empty line
line = line.rstrip()
if 'sub string' in line:
count += 1
print(count, line)
# Close the file
fhand.close()