title: WindowsError: [Error 3] The system cannot find the path specified (when path too long?)
body: <p><strong>WindowsError: [Error 3] The system cannot find the path specified (when path too long?)</strong></p>
  
  <p>I'm making a script to find unique files between two directories. In order to do this, I use <code>os.walk()</code> to walk through the files, and if files of the same size exist, I hash them to ensure they are the same (opening the files in the process). The problem is that <strong>some</strong> files produce the above-mentioned error when opened. The most common reason people run into this problem is because the path is not correctly joined, thereby causing the script to try and open a file that doesn't exist. This isn't the case for me.</p>
  
  <p>After trying different combinations of directories, I began to notice a pattern whereby files that produce the error seem to have a deep directory structure and a long filename. I can't think of any other reason for the issue - there are no character-encoding errors (I decode all my paths to UTF-8) and the paths do exist by virtue of <code>os.walk()</code>. </p>
  
  <p>My walk code:</p>
  
  <pre><code>for root, dirs, files in os.walk(directory):
      for filename in files:
          file_path = os.path.join(root, filename)
  </code></pre>
  
  <p>My hashing code:</p>
  
  <pre><code>def hash(file_path):
      with open(dir_file, 'rb') as f:
          hasher = hashlib.md5()
          while True:
              buf = f.read(byte_size)
              if buf != '':
                  hasher.update(buf)
              else:
                  break
          result = hasher.hexdigest()
      return result
  </code></pre>
  
  <p><em>Edit:</em> The most recent path where the issue appeared was 5 directories deep (containing 142 characters, accounting for double backslashes), and the filename was an additional 122 characters long</p>