Python File handling: standard input and output -


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

in above code, shouldn't cat in file after execution? however, when run it, find file empty. if code wrong, can explain how sys.stdin.read() , sys.stdout.write() work , uses?

you need close file

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

if want see updated file content before closing file or exiting program can use flush():

fileobject.flush() 

check stackoverflow question standard input/output


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -