错误文件未打开以供阅读

时间:2013-10-17 14:08:54

标签: python

我正在使用以下脚本将副本权限添加到目录和子目录中的所有文件中,作为第一个参数传递的给定目录,运行脚本如下,但运行到下面的错误... 任何人都可以提供关于修复它的输入吗?

错误: -

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

代码: -

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)

1 个答案:

答案 0 :(得分:1)

您可能希望将shutil.copyfileobj(in_file, out_file)try ... except块一起包装,以找出导致问题的特定文件。这听起来像是一个非典型的权限问题 - 通常会抛出IOError: [Errno 13] Permission denied - 或者你可能在磁盘上的一个输入文件中有损坏。 (这个脚本在OS X,Windows和Windows / Cygwin上都适用于我。)