@@ -370,14 +370,14 @@ Format
370
370
```
371
371
372
372
#### 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
376
376
377
377
#### Integer presentation types:
378
- * ` 'c' ` - Character
379
- * ` 'b' ` - Binary
380
- * ` 'x' ` - Hex
378
+ * ` 'c' ` - character
379
+ * ` 'b' ` - binary
380
+ * ` 'x' ` - hex
381
381
* ` 'X' ` - HEX
382
382
383
383
@@ -822,14 +822,46 @@ script_name = sys.argv[0]
822
822
arguments = sys.argv[1 :]
823
823
```
824
824
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:
826
858
``` python
827
859
def read_file (filename ):
828
860
with open (filename, encoding = ' utf-8' ) as file :
829
861
return file .readlines()
830
862
```
831
863
832
- ### Write to File
864
+ #### Write to Text File:
833
865
``` python
834
866
def write_to_file (filename , text ):
835
867
with open (filename, ' w' , encoding = ' utf-8' ) as file :
@@ -867,22 +899,6 @@ b'.\n..\nfile1.txt\nfile2.txt\n'
867
899
0
868
900
```
869
901
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
-
886
902
### Recursion Limit
887
903
``` python
888
904
>> > import sys
0 commit comments