-2

The following code is should write the line "Hello World" in a output.txt file but doesn't, how do i correct it if its wrong?

import sys,os
if not os.environ.get("ONLINE_JUDGE"):
    sys.stdin=open('./input.txt','r')
    sys.stdout=open('./output.txt','w')
import time
start_time=time.time()

print("Hello World")

print("______ %s seconds ____"%(time.time()-start_time))

Akash Kp
  • 11
  • 4

1 Answers1

0

The following will give you write/read capability for .txt files, with the added benefit of not requiring any import statements, and is, in my opinion, easier to use.

with open("{0}.txt".format(fullfilepath), mode = 'w+') as output:
   output.write("Hello World!\n")
  • 1
    When you say "try" it sounds like you don't know if your answer will work or not. I'd suggest being more confident-sounding in your answers. – Random Davis Jan 25 '23 at 16:26
  • @RandomDavis, how's that sound? – Jacob Ivanov Jan 25 '23 at 16:31
  • Yeah the language is a lot more confident. Not sure if this actually really fully answers the question as this is not quite equivalent code, and we're not exactly sure why the asker was trying to do things with stdin and stdout, but that doesn't mean this is not a useful and helpful answer still. It may very well be all they needed to know. – Random Davis Jan 25 '23 at 17:04