Python: printing a file to stdout

后端 未结 8 1959
眼角桃花
眼角桃花 2020-12-08 03:55

I\'ve searched and I can only find questions about the other way around: writing stdin to a file :)

Is there a quick and easy way to dump the contents of a file to s

8条回答
  •  情歌与酒
    2020-12-08 04:09

    f = open('file.txt', 'r')
    print f.read()
    f.close()
    

    From http://docs.python.org/tutorial/inputoutput.html

    To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").

提交回复
热议问题