Skip to content

Commit 027020b

Browse files
committed
Open
1 parent f1ed710 commit 027020b

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

README.md

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ Format
370370
```
371371

372372
#### Float presentation types:
373-
* `'f'` - Fixed point: `.<precision>f`
374-
* `'%'` - Percent: `.<precision>%`
375-
* `'e'` - Exponent
373+
* `'f'` - fixed point: `.<precision>f`
374+
* `'%'` - percent: `.<precision>%`
375+
* `'e'` - exponent
376376

377377
#### Integer presentation types:
378-
* `'c'` - Character
379-
* `'b'` - Binary
380-
* `'x'` - Hex
378+
* `'c'` - character
379+
* `'b'` - binary
380+
* `'x'` - hex
381381
* `'X'` - HEX
382382

383383

@@ -822,14 +822,46 @@ script_name = sys.argv[0]
822822
arguments = sys.argv[1:]
823823
```
824824

825-
### Read File
825+
### Input
826+
**Reads a line from user input or pipe if present. The trailing newline gets stripped.**
827+
828+
```python
829+
filename = input('Enter a file name: ')
830+
```
831+
832+
#### Prints lines until EOF:
833+
```python
834+
while True:
835+
try:
836+
print(input())
837+
except EOFError:
838+
break
839+
```
840+
841+
### Open
842+
**Opens file and returns a corresponding file object.**
843+
844+
```python
845+
<file> = open(<path>, mode='r', encoding=None)
846+
```
847+
848+
#### Modes:
849+
* `'r'` - open for reading (default)
850+
* `'w'` - open for writing, erasing the file first
851+
* `'x'` - open for exclusive creation, failing if the file already exists
852+
* `'a'` - open for writing, appending to the end of the file if it exists
853+
* `'b'` - binary mode
854+
* `'t'` - text mode (default)
855+
* `'+'` - open a disk file for updating (reading and writing)
856+
857+
#### Read Text File:
826858
```python
827859
def read_file(filename):
828860
with open(filename, encoding='utf-8') as file:
829861
return file.readlines()
830862
```
831863

832-
### Write to File
864+
#### Write to Text File:
833865
```python
834866
def write_to_file(filename, text):
835867
with open(filename, 'w', encoding='utf-8') as file:
@@ -867,22 +899,6 @@ b'.\n..\nfile1.txt\nfile2.txt\n'
867899
0
868900
```
869901

870-
### Input
871-
**Reads a line from user input or pipe if present. The trailing newline gets stripped.**
872-
873-
```python
874-
filename = input('Enter a file name: ')
875-
```
876-
877-
#### Prints lines until EOF:
878-
```python
879-
while True:
880-
try:
881-
print(input())
882-
except EOFError:
883-
break
884-
```
885-
886902
### Recursion Limit
887903
```python
888904
>>> import sys

0 commit comments

Comments
 (0)