Writing to a file prints integers to my IDLE shell

删除回忆录丶 提交于 2019-12-11 11:52:53

问题


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.

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))

while this doesn't:

>> def print_to_file():
       file = open('filename', 'w')
       for element in list:
           file.write('{}\n'.format(element))
       file.close()
>> print_to_file()

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.


回答1:


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.



来源:https://stackoverflow.com/questions/29114077/writing-to-a-file-prints-integers-to-my-idle-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!