-1
import sys 

fileobject=open('file.txt','w')   
fileobject.write(sys.stdin.readline())
Cat

In the above code, shouldn't cat be in file after execution? However, when I run it, I find file empty. If my code is wrong, can someone explain how sys.stdin.read() and sys.stdout.write() work and their uses?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Mahathi Vempati
  • 1,238
  • 1
  • 12
  • 33

1 Answers1

1

You need to close the file

import sys 

fileobject = open('file.txt', 'w')   
fileobject.write(sys.stdin.readline())
Cat
fileobject.close()

If you want to see the updated file content before closing the file or exiting the program you can use flush():

fileobject.flush()

Check this StackOverflow Question for standard input/output

Community
  • 1
  • 1