python打印空行_python在写入文件时在第一行打印一个空行

我被困在为什么我的代码在向文件写入文本之前打印空行。我正在做的是从压缩文件夹中读取两个文件并将文本写入新的文本文件。我在文件中获得了预期的结果,除了文件第一行有空行外。

def test():

if zipfile.is_zipfile(r'C:\Users\test\Desktop\Zip_file.zip'):

zf = zipfile.ZipFile(r'C:\Users\test\Desktop\Zip_file.zip')

for filename in zf.namelist():

with zf.open(filename, 'r') as f:

words = io.TextIOWrapper(f)

new_file = io.open(r'C:\Users\test\Desktop\new_file.txt', 'a')

for line in words:

new_file.write(line)

new_file.write('\n')

else:

pass

zf.close()

words.close()

f.close()

new_file.close()在new_file中输出(在第一个“This is a test line ...”前有一个空行)

This is a test line...

This is a test line...

this is test #2

this is test #2有任何想法吗?

谢谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值