为什么我的Python脚本没有将最后几行写入文件中? [英] Why is my Python script not writing the last few lines into my file?

查看:203
本文介绍了为什么我的Python脚本没有将最后几行写入文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试从一个文件中读取数字列表,将其拆分,然后放入另一个文件中.在弄乱了一些调试打印后,我得出的结论是,问题不在于字符串的循环或拆分,而在于脚本的最后一行,实际上我转到新文件.

I've been trying to read lists of numbers from one file, split them up, and put them into another file. After messing with a few debug prints, I've come to the conclusion that the issue isn't with the looping or splitting of my strings, but rather with the very last line of the script where I actually write to the new file.

它不仅可以按照我的意愿进行写入,而且可以在文件中获得大部分访问权限,然后不写入文件的最后几行.我可以在脚本中编写的东西数量是否有限制?还是这里还有其他事情?

Rather than just write as I'd want it to, it gets most of the way through the file, and then just doesn't write the last few lines of the file. Is there some limit to the number of things I can write in a script? Or is something else going on here?

这是脚本: 导入字符串

Here's the script: import string

#constants to name out in/out files
INFILE = 'newkicBright.txt'
OUTFILE = 'out.txt'

#open both files
inHandle = open(INFILE, 'r')
outHandle = open(OUTFILE, 'w')

#console verifies that everything's opened
print inHandle
print outHandle

#read our data into the file!
for line in inHandle:
    nums = string.split(line)
    for num in nums:
        num += " PLACEHOLDER\n"
        outHandle.write(num)

推荐答案

您需要关闭文件处理程序,以便它实际写入并清除所有内容.

You need to close your file handler so it actually writes and cleans everything up.

outHandle.close()

将其添加到末尾.

close暗示flush: Python?

这篇关于为什么我的Python脚本没有将最后几行写入文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆