title: Why does a Python script to read files cause my computer to emit beeping sounds?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>To start it, you pass <code>init_crawl()</code> the directory to crawl down from, the string to search for, and a directory to write an output text file into. For example: <code>init_crawl(r'C:\directory-to-crawl', 'foo', r'C:\output-directory')</code></p>
  
  <p>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...</p>
  
  <p>The output is just a csv with columns for file paths where the string is found, line numbers, and the line containing the string.</p>