diff --git a/README.md b/README.md index 709ec3f..0660b92 100644 --- a/README.md +++ b/README.md @@ -370,14 +370,14 @@ Format ``` #### Float presentation types: -* `'f'` - Fixed point: `.f` -* `'%'` - Percent: `.%` -* `'e'` - Exponent +* `'f'` - fixed point: `.f` +* `'%'` - percent: `.%` +* `'e'` - exponent #### Integer presentation types: -* `'c'` - Character -* `'b'` - Binary -* `'x'` - Hex +* `'c'` - character +* `'b'` - binary +* `'x'` - hex * `'X'` - HEX @@ -822,14 +822,46 @@ script_name = sys.argv[0] arguments = sys.argv[1:] ``` -### Read File +### Input +**Reads a line from user input or pipe if present. The trailing newline gets stripped.** + +```python +filename = input('Enter a file name: ') +``` + +#### Prints lines until EOF: +```python +while True: + try: + print(input()) + except EOFError: + break +``` + +### Open +**Opens file and returns a corresponding file object.** + +```python + = open(, mode='r', encoding=None) +``` + +#### Modes: +* `'r'` - open for reading (default) +* `'w'` - open for writing, erasing the file first +* `'x'` - open for exclusive creation, failing if the file already exists +* `'a'` - open for writing, appending to the end of the file if it exists +* `'b'` - binary mode +* `'t'` - text mode (default) +* `'+'` - open a disk file for updating (reading and writing) + +#### Read Text File: ```python def read_file(filename): with open(filename, encoding='utf-8') as file: return file.readlines() ``` -### Write to File +#### Write to Text File: ```python def write_to_file(filename, text): with open(filename, 'w', encoding='utf-8') as file: @@ -867,22 +899,6 @@ b'.\n..\nfile1.txt\nfile2.txt\n' 0 ``` -### Input -**Reads a line from user input or pipe if present. The trailing newline gets stripped.** - -```python -filename = input('Enter a file name: ') -``` - -#### Prints lines until EOF: -```python -while True: - try: - print(input()) - except EOFError: - break -``` - ### Recursion Limit ```python >>> import sys