Writing to a file prints integers to my IDLE shell

2019-08-02 13:46发布

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条回答
\"骚年 ilove
2楼-- · 2019-08-02 13:59

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.

查看更多
登录 后发表回答