I wrote a little module that will search files in a directory and all of its sub-directories for the occurrence of some input string. It's been handy a few times, mostly to find old scripts if I remember some function/variable name that I used.
So, I was completely baffled the other day when I used the functions and started hearing, very faintly, from the headphones sitting on the far side of my desk, repeated beeping sounds. At first I thought it was somebody's phone ringing. But no, Python was communicating with me via Morse code.
I have no clue why this is happening. I've continued running the functions and getting beeps, not always in the same pattern. The functions only open files with read permission. The code is exactly this:
import os
import glob
def directory_crawl_for_string(dir_name, string, ofile):
"""Crawl dir_name and all of its subdirectories, opening files and
checking for the presence of a string"""
#get input directory's listings
dir_contents = glob.glob(dir_name)
#loop over the listings
for dir_element in dir_contents:
if(os.path.isfile(dir_element)):
#read the file, checking for the string
check_for_string(dir_element, string, ofile)
else:
if(os.path.isdir(dir_element)):
directory_crawl_for_string(dir_element + '\\*', string, ofile)
def check_for_string(dir_element, string, ofile):
try:
ifile = open(dir_element, 'r')
except IOError as e:
pass
else:
count = 1
for line in ifile:
if(string in line.lower()):
print count,line,dir_element
ofile.write('%s,%d,%s' % (dir_element, count, line))
count += 1
ifile.close()
def init_crawl(start_dir, string, output_dir):
"""args:
start_dir - directory to start the crawl at
string - string to search for
output_dir - directory to write output text file inside of"""
if(output_dir):
fn = output_dir.rstrip('/').rstrip('\\') + '/dirs.txt'
else:
fn = 'dirs.txt'
ofile = open(fn, 'w')
ofile.write('file path,line number of occurance of "%s",exact line\n' % string)
directory_crawl_for_string(start_dir, string, ofile)
ofile.close()
print('list of files containing "%s" written to "%s"' % (string, fn))
To start it, you pass init_crawl()
the directory to crawl down from, the string to search for, and a directory to write an output text file into. For example: init_crawl(r'C:\directory-to-crawl', 'foo', r'C:\output-directory')
I don't even know what specific questions to ask about this, but why is it happening? I can tell that the beeps generally occur when the function tries to read non-text files like PDFs and spreadsheets. Sometimes the terminal freezes too...
The output is just a csv with columns for file paths where the string is found, line numbers, and the line containing the string.
This line:
print count,line,dir_element
is probably printing the BEL character if you feed your program binary files.
To test, here's a little code I wrote. Python will try and play it note-for-note. Don't worry. Be happy :)
def bel(): return chr(7)
def wait(duration): return chr(0) * (duration*1000000)
song = ''
for _ in range(5):
song += bel()
song += wait(1)
song += bel()
song += wait(1)
song += bel()
song += wait(5)
print song
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With