Add lines 109-118(reading files) (#27795)
* Add lines 109-118(reading files) * Update index.mdpull/27749/head
parent
7463e75530
commit
218bcfe75d
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue