python新建文件保存不上_无法创建文件,因为该文件已存在使用Python观察到的错误...

我试图重命名文件,然后打开它并修改数据,但我观察到以下错误

[错误183]当文件已存在时,无法创建该文件

我的代码在这里:def copy_and_replace_new_files(path, list_of_dictionary):

''' get to working directory'''

os.chdir(path)

''' get each dictionary

get all key values

start moving files to new destination

this will be with new name'''

for item in iter(list_of_dictionary):

''' join path and source file

copy to destination and rename'''

src_dir = os.curdir

dst_dir = os.path.join(os.curdir, "new")

src_file = os.path.join(src_dir, item['filename'])

shutil.copy(src_file, dst_dir)

dst_file = os.path.join(dst_dir, item['filename']) # old file name

new_file_name = os.path.splitext(item['filename'])[0]

new_file_name = new_file_name + '_new' + '.txt'

os.rename(dst_file, new_file_name)

''' Find and Replace with Pattern'''

for newline in fileinput.FileInput(new_file_name, inplace=1):

pattern = item['table']

pattern = str(pattern).rstrip()

newline = newline.replace(str(pattern), str(pattern+'_new'))

print(newline.rstrip())

你能告诉我哪里出错了吗。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值