写入文件会将整数打印到我的 IDLE shell [英] Writing to a file prints integers to my IDLE shell
问题描述
写入文件会将整数打印到我的 IDLE shell.它们似乎在 15 到 40 之间,而且我的文件中打印的每一行都有一个.
Writing to a file prints integers to my IDLE shell. They seem to range from 15-40 and there's one for every line printed to my file.
这仅发生在我直接在 IDLE 中、在函数之外编写语句时.这会导致整数打印:
This only occurs if I write the statement directly in IDLE, outside of a function. This causes the integers to print:
>> file = open('filename', 'w')
>> for element in list:
file.write('{}\n'.format(element))
虽然没有:
>> def print_to_file():
file = open('filename', 'w')
for element in list:
file.write('{}\n'.format(element))
file.close()
>> print_to_file()
我在 Windows 7 上使用 IDLE 3.4.0.我还没有机会在另一台机器或其他版本的 IDLE 上测试它.
I'm using IDLE 3.4.0 on Windows 7. I haven't had the opportunity to test it on another machine or another version of IDLE.
推荐答案
当您在交互式解释器中执行语句并且该语句是返回值的表达式时,将显示结果.write()
返回写入的字节数.
When you execute a statement in the interactive interpreter and that statement is an expression that returns a value, the result will be displayed. write()
returns how many bytes were written.
这篇关于写入文件会将整数打印到我的 IDLE shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!