1
我被困在为什么我的代码在将文本写入文件之前打印空行。我正在做的是从压缩文件夹中读取两个文件并将文本写入新的文本文件。我在文件中获得了预期的结果,除了文件第一行有空行外。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 test #2
this is test #2
任何想法?
谢谢!
我认为你是对的。当我调用zf.namelist()时,我得到以下列表。 ['New folder /','New folder/test1.txt','New folder/test2.txt']。新文件夹是保存文件的文件夹的名称。我会回到文档,看看是否有一个好方法。 –