2013-10-17 39 views
2

我正在使用以下腳本將目錄和子目錄中的所有文件的副本權添加到作爲第一個參數傳遞的give目錄中,我按如下方式運行腳本,但遇到以下錯誤。 ..任何人都可以提供輸入ohw來解決它?錯誤文件未打開可供閱讀

錯誤: -

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) 
+1

嘗試向'NamedTemporaryFile'添加'mode ='rw + b''。 –

+0

你確定你仍然有這個錯誤嗎?我試過'prepend_file'和'shutil.copyfileobj'這一節沒有引發任何錯誤。 –

+0

@BrianCain - 我將模式更改爲「rw + b」,如下所示..我得到一個錯誤「ValueError:無效模式('rw + b')」#使用二進制模式來避免換行 帶有開放(全名, rw + b「)作爲in_file: – user2125827

回答

1

你不妨換shutil.copyfileobj(in_file, out_file)try ... except塊,找出具體的文件引起的問題。這聽起來像是非典型權限問題—,通常會拋出IOError: [Errno 13] Permission denied —或者您可能在磁盤上的輸入文件之一中有損壞。 (這個腳本適用於OS X,Windows和Windows/Cygwin。)