kiasy
kiasy

Reputation: 304

Printing to file works on Linux but not on Windows?

So I wrote this code and it works perfectly fine on Linux.

  1. Reading data from file
  2. Do whatever my code is supposed to do
  3. Write the solutions on a new file.

Here's the part of the code that is supposed to do that:

outFile = open( "input.txt", "w" )

for item in oplist:
     outFile.write(item + "\n")

outFile.close

It works perfectly fine on Linux but on windows in only creates the new output file but doesn't write anything into it.

Please help!

Upvotes: 0

Views: 162

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124768

You are not closing the file; you are merely referring to the close method. Call it:

outFile.close()

Without closing the file buffers won't be flushed until Python exits.

A better way to handle file closing is to use the with statement:

with open( "input.txt", "w" ) as outFile:
    for item in oplist:
        outFile.write(item + "\n")

Now the file is closed automatically.

Upvotes: 2

Related Questions