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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -