Checking for some exact lines in each file in directory and subdirectories
Question: I am trying to do the following and running into errors,what is going wrong?

1.Open each file in the directory and subdirectories based on the argument

2.Check if each file has the copyright info exactly the 3 lines

    
    
     Copyright (c) 2012 Company, Inc. 
     All Rights Reserved.
     Company Confidential and Proprietary.
    

The code:

    
    
    import os
    import sys
    userstring="Copyright (c) 2012 Company, Inc.\nAll Rights Reserved.\nCompany Confidential and Proprietary."
    print len(sys.argv)
    print sys.argv[1]
    if len(sys.argv) < 2:
        sys.exit('Usage: python.py <build directory>')
    for r,d,f in os.walk(sys.argv[1]):
        for files in f:
            with open(os.path.join(r, files), "r") as file:
                if ''.join(file.readlines()[:3]).strip() != userstring:
                    print files
    


Answer: Maybe by changing these lines:

    
    
    file = open(files, "r")
    if userstring not in line:  #check if each file contains the lines
        print file
    

To these:

    
    
    with open(os.path.join(r, files), "r") as file:
        if ''.join(file.readlines()[:3]).strip() != userstring:
            print files
    

You achieve your goal. But when you're working with files, you should be aware
about bad files bad formatting and exception handling and etc.

