python3创建文件失败_python-WindowsError:[错误183]该文件已存在时无法创建文件

我正在使用以下脚本向目录和子目录中的所有文件添加复制权,以作为作为第一个参数传递的给定目录,如下运行脚本,但遇到以下错误…

谁能提供有关修复的信息?

错误:-

C:\Dropbox\copyrights>python Add_copyright.py .

Traceback (most recent call last):

File "Add_copyright.py", line 69, in

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

码:-

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)

解决方法:

如果要追加到现有文件,则需要类似以下内容的文件:

打开(全名,“ ab”)作为in_file

问:您确定可以将shutil.copyfileobj(in_file,out_file)与已写入的打开的out_file一起使用吗?

问:您知道哪条线导致Windows错误吗?

谢谢你的更新.

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

标签:python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值