2

I am using the following script to add a copy right to all the files in the directories and subdirectories for a give directory passed as first argument ,am running the script as follows but running into below error... can anyone provide inputs on ohw to fix it?

ERROR:-

C:\Dropbox\copyrights>python Add_copyright.py .
Traceback (most recent call last):
  File "Add_copyright.py", line 70, in <module>
    prepend_file(fullname, dirpath)
  File "Add_copyright.py", line 50, in prepend_file
    shutil.copyfileobj(in_file, out_file)
  File "C:\Python27\lib\shutil.py", line 49, in copyfileobj
    buf = fsrc.read(length)
IOError: File not open for reading

CODE:-

import fnmatch
import os
import shutil
import sys
import tempfile

file_patterns_to_match = ['*.c','*.h','*.cpp','*.txt']

headertext = """/*
 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
 *
 * Previously licensed under the ISC license by Company, Inc.
 *
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */
"""

# make any newlines in headertext match the system line ending
headertext = headertext.replace('\n', os.linesep)

def want_this_file(fname):
    for pat in file_patterns_to_match:
        if fnmatch.fnmatch(fname, pat):
            return True
    return False

def prepend_file(fullname, path):
    # with statement means temp file is written and closed at end of with
    with tempfile.NamedTemporaryFile(dir=path, delete=False) as out_file:
        # get the name immediately
        temp_fname = out_file.name

        try:
            # use binary mode to avoid newline translations
            with open(fullname, "rb") as in_file:
                out_file.write(headertext)
                shutil.copyfileobj(in_file, out_file)
        except Exception:
            # on any error, clean up temp file and re-raise exception
            try:
                os.remove(temp_fname)
            except Exception:
                print("unable to clean up temp file: " + temp_fname)
                pass
            raise
    # rename temp file to fullname, clobbering original
    os.rename(temp_fname, fullname)


start_directory = sys.argv[1]

for dirpath, dirnames, filenames in os.walk(start_directory):
    for fname in filenames:
        if want_this_file(fname):
            fullname = os.path.join(dirpath, fname)
            prepend_file(fullname, dirpath)
5
  • 1
    try to add mode='rw+b' to the NamedTemporaryFile. Commented Oct 17, 2013 at 14:53
  • Are you sure you're still getting that error? I tried that section from prepend_file and shutil.copyfileobj didn't raise any errors. Commented Oct 17, 2013 at 15:33
  • @BrianCain - I changed the mode to "rw+b" as follows..i get an error "ValueError: Invalid mode ('rw+b')" # use binary mode to avoid newline translations with open(fullname, "rw+b") as in_file: Commented Oct 17, 2013 at 15:56
  • @CristianCiupitu - can you tell me how you ran the script..am still getting the error Commented Oct 17, 2013 at 15:56
  • @user2125827 gist.github.com/ciupicri/7028074 Commented Oct 17, 2013 at 16:36

1 Answer 1

1

You may wish to wrap shutil.copyfileobj(in_file, out_file) with a try ... except block to find out what specific file is causing the problem. It sounds like either an atypical permissions issue — which would normally throw IOError: [Errno 13] Permission denied — or you may have corruption in one of the input files on disk. (This script is working for me on both OS X, Windows, and Windows/Cygwin.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.