0

Writing from file_A to file_B using IDLE always makes IDLE print out the lines as they are being written. If the file is very large, then the process would take hours to finish.

How can I make IDLE not print anything while the process of writing to a new file is ongoing, in order to speed things up?

A simple code to demonstrate that IDLE prints the lines as they are being written:

file = open('file.csv','r')
copy = open('copy.csv','w')
for i in file:
    i = i.split()
    copy.write(str(i))
16
  • 1
    I would not expect this to generally be the case; are you sure? Could you show a minimal example that displays this behaviour? Commented Mar 18, 2015 at 17:32
  • IDLE doesn't print anything as lines are being written, so your problem is already solved. Commented Mar 18, 2015 at 17:32
  • 1
    @Dorky I have literally used IDLE to write to a file, without seeing the behaviour you describe. If you provide links to e.g. imgur uploads I can add them to your question. Commented Mar 18, 2015 at 17:47
  • 2
    @Dorky I am using IDLE, I have used your test code, I cannot replicate your problem: imgur.com/ugl2vMU. Note also that you should use the with context manager (see the earlier example in my screenshot) to handle files more neatly. Commented Mar 18, 2015 at 17:53
  • 1
    Idle executes code in a separate python user process. Its Shell can only print what python sends back to it. There may be an error in your copy of python, and you should re-install. You might have triggered an obscure bug in 3.x's file.write method. If you have a reproducible example, you should execute it directly in Python, without going through Idle. A better place to dicsuss this would python-list, mail.python.org/mailman/listinfo/python-list, also accessible as gmane.comp.python.general at news.gmane.org. Commented Mar 18, 2015 at 19:26

1 Answer 1

1

I assume you are using Python3 where write returns the number of characters written to the file and IDLE's python shell prints this return value when you call it. In Python2 write returns None that is not printed by IDLE's shell.

The workaround is to assign the return value of write to a temporary dummy variable

dummy = f.write("my text")

For your example the following code should work

file = open('file.csv','r')
copy = open('copy.csv','w')
for i in file:
    i = i.split()
    dummy = copy.write(str(i))

I added two screenshots for all of you to see the difference between the writes in Python 2 and Python 3 on my system.

enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

16 Comments

It should not print the return value when called from within a loop though.
@halex Yes, I am using Python 3.4. Yeah, those are probably bytes, but for larger works IDLE will print the entire lines exactly and this really takes up tremendous amount of time.
@Dorky so you get different behaviour for different sizes of file? Are you using Python 3.x? Again, including as much information as you can in the question will help figure out what's going on.
@halex Frankly I have no idea how to write a complete code of using that dummy variable, but I am going to experiment on this. I hope you can teach me how to incorporate that dummy code into the loop (or elsewhere) to make it work.
Sorry, it does print out the value even within a loop - I stand corrected.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.