Add lines 109-118(reading files) (#27795)

* Add lines 109-118(reading files)

* Update index.md
pull/27749/head
ishan-sriv 2019-03-03 23:41:21 +05:30 committed by Randell Dawson
parent 7463e75530
commit 218bcfe75d
1 changed files with 10 additions and 0 deletions

View File

@ -110,6 +110,16 @@ Sample code to open a text file using the `with` statement:
with open('hello_world.txt', 'w') as f:
f.write('Hello World!')
```
#### Reading files
There are many ways of reading a file. Reading a file <code>fh</code> is only possible if file is opened in read mode, the following are the options:
<code>fh.read()</code>: read file as a single string
<code>fh.read(n)</code>: read n number of characters
<code>fh.readline()</code>: read one line and end with <code>\n</code>
<code>fh.readlines()</code>: read file as a list of strings and end with <code>'\n'</code>
#### More Information:
[Python Documentation - IO](https://docs.python.org/2/tutorial/inputoutput.html)