2013-10-17 107 views
3

我正在使用以下脚本将目录和子目录中的所有文件的副本权添加到作为第一个参数传递的give目录中,脚本如下,但遇到以下错误... 任何人都可以提供输入ohw来解决它?WindowsError:[错误183]当该文件已存在时无法创建文件

错误: -

C:\Dropbox\copyrights>python Add_copyright.py . 
Traceback (most recent call last): 
    File "Add_copyright.py", line 69, in <module> 
    prepend_file(fullname, dirpath) 
    File "Add_copyright.py", line 60, in prepend_file 
    os.rename(temp_fname, fullname) 
WindowsError: [Error 183] Cannot create a file when that file already exists 

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 Qualcomm Atheros, 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

Windows上的文件IO很痛苦......不直观且不一致。 –

回答

3

如果您要附加到现有的文件,你需要的东西,如:

with open(fullname, "ab") as in_file

问:你确定你可以使用shutil.copyfileobj(in_file, out_file)带有你已经写好的开放式文件赶到?

问:你知道哪一行导致windows错误吗?

感谢您的更新。

我打赌你的目录已经有一个文件已经命名为fullname,因此os.rename(temp_fname, fullname)失败,“文件已经存在”。

问:请问shutil.move的工作原理是什么?

+0

是......错误显示哪一行导致此问题 – user2125827

+0

问:您是否能够通过确保1)该文件在重命名临时文件之前不存在,或2)“移动”而不是“重命名”覆盖原始文件是什么? – paulsm4

+1

- 我试过shutil.move而不是os.rename ..问题依然存在.. – user2125827

相关问题