@@ -888,7 +888,7 @@ while True:
888
888
break
889
889
```
890
890
891
- #### Raising exception:
891
+ ### Raising Exception
892
892
``` python
893
893
raise ValueError (' A very specific message!' )
894
894
```
@@ -906,16 +906,15 @@ KeyboardInterrupt
906
906
```
907
907
908
908
909
- System
910
- ------
911
- ### Print Function
909
+ Print
910
+ -----
912
911
``` python
913
912
print (< el_1> , ... , sep = ' ' , end = ' \n ' , file = sys.stdout, flush = False )
914
913
```
915
914
916
915
* ** Use ` 'file=sys.stderr' ` for errors.**
917
916
918
- #### Pretty print:
917
+ ### Pretty Print
919
918
``` python
920
919
>> > from pprint import pprint
921
920
>> > pprint(dir ())
@@ -924,7 +923,9 @@ print(<el_1>, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
924
923
' __doc__' , ... ]
925
924
```
926
925
927
- ### Input Function
926
+
927
+ Input
928
+ -----
928
929
* ** Reads a line from user input or pipe if present.**
929
930
* ** The trailing newline gets stripped.**
930
931
* ** The prompt string is printed to standard output before reading input.**
@@ -942,14 +943,16 @@ while True:
942
943
break
943
944
```
944
945
945
- ### Open Function
946
+
947
+ Open
948
+ ----
946
949
** Opens file and returns a corresponding file object.**
947
950
948
951
``` python
949
952
< file > = open (' <path>' , mode = ' r' , encoding = None )
950
953
```
951
954
952
- #### Modes:
955
+ ### Modes
953
956
* ** ` 'r' ` - Read (default).**
954
957
* ** ` 'w' ` - Write (truncate).**
955
958
* ** ` 'x' ` - Write or fail if the file already exists.**
@@ -960,80 +963,30 @@ while True:
960
963
* ** ` 't' ` - Text mode (default).**
961
964
* ** ` 'b' ` - Binary mode.**
962
965
963
- #### Seek:
966
+ ### Seek
964
967
``` python
965
968
< file > .seek(0 ) # Move to start of the file.
966
969
< file > .seek(offset) # Move 'offset' chars/bytes from the start.
967
970
< file > .seek(offset, < anchor> ) # Anchor: 0 start, 1 current pos., 2 end.
968
971
```
969
972
970
- #### Read Text from File:
973
+ ### Read Text from File
971
974
``` python
972
975
def read_file (filename ):
973
976
with open (filename, encoding = ' utf-8' ) as file :
974
977
return file .readlines()
975
978
```
976
979
977
- #### Write Text to File:
980
+ ### Write Text to File
978
981
``` python
979
982
def write_to_file (filename , text ):
980
983
with open (filename, ' w' , encoding = ' utf-8' ) as file :
981
984
file .write(text)
982
985
```
983
986
984
- ### Command Execution
985
- ``` python
986
- import os
987
- < str > = os.popen(< command> ).read()
988
- ```
989
-
990
- #### Or:
991
- ``` python
992
- >> > import subprocess
993
- >> > a = subprocess.run([' ls' , ' -a' ], stdout = subprocess.PIPE )
994
- >> > a.stdout
995
- b ' .\n ..\n file1.txt\n file2.txt\n '
996
- >> > a.returncode
997
- 0
998
- ```
999
-
1000
- ### Recursion Limit
1001
- ``` python
1002
- >> > import sys
1003
- >> > sys.getrecursionlimit()
1004
- 1000
1005
- >> > sys.setrecursionlimit(5000 )
1006
- ```
1007
-
1008
-
1009
- Command Line Arguments
1010
- ----------------------
1011
- ### Basic
1012
- ``` python
1013
- import sys
1014
- script_name = sys.argv[0 ]
1015
- arguments = sys.argv[1 :]
1016
- ```
1017
-
1018
- ### Argparse
1019
- ``` python
1020
- from argparse import ArgumentParser, FileType
1021
- p = ArgumentParser(description = < str > )
1022
- p.add_argument(' -<short_name>' , ' --<name>' , action = ' store_true' ) # Flag
1023
- p.add_argument(' -<short_name>' , ' --<name>' , type = < type > ) # Option
1024
- p.add_argument(' <name>' , type = < type > , nargs = 1 ) # Argument
1025
- p.add_argument(' <name>' , type = < type > , nargs = ' +' ) # Arguments
1026
- args = p.parse_args()
1027
- value = args.< name>
1028
- ```
1029
-
1030
- * ** Use ` 'help=<str>' ` for argument description.**
1031
- * ** Use ` 'type=FileType(<mode>)' ` for files.**
1032
-
1033
987
1034
988
Path
1035
989
----
1036
- ### Basic
1037
990
``` python
1038
991
from os import path, listdir
1039
992
< bool > = path.exists(' <path>' )
@@ -1048,7 +1001,9 @@ from os import path, listdir
1048
1001
[' 1.gif' , ' card.gif' ]
1049
1002
```
1050
1003
1051
- ### Pathlib
1004
+
1005
+ Pathlib
1006
+ -------
1052
1007
** This module offers classes representing filesystem paths with semantics appropriate for different operating systems.**
1053
1008
1054
1009
``` python
@@ -1080,6 +1035,59 @@ cwd = Path()
1080
1035
```
1081
1036
1082
1037
1038
+ Command Line Arguments
1039
+ ----------------------
1040
+ ### Basic
1041
+ ``` python
1042
+ import sys
1043
+ script_name = sys.argv[0 ]
1044
+ arguments = sys.argv[1 :]
1045
+ ```
1046
+
1047
+ ### Argparse
1048
+ ``` python
1049
+ from argparse import ArgumentParser, FileType
1050
+ p = ArgumentParser(description = < str > )
1051
+ p.add_argument(' -<short_name>' , ' --<name>' , action = ' store_true' ) # Flag
1052
+ p.add_argument(' -<short_name>' , ' --<name>' , type = < type > ) # Option
1053
+ p.add_argument(' <name>' , type = < type > , nargs = 1 ) # Argument
1054
+ p.add_argument(' <name>' , type = < type > , nargs = ' +' ) # Arguments
1055
+ args = p.parse_args()
1056
+ value = args.< name>
1057
+ ```
1058
+
1059
+ * ** Use ` 'help=<str>' ` for argument description.**
1060
+ * ** Use ` 'type=FileType(<mode>)' ` for files.**
1061
+
1062
+
1063
+ Command Execution
1064
+ -----------------
1065
+ ``` python
1066
+ import os
1067
+ < str > = os.popen(< command> ).read()
1068
+ ```
1069
+
1070
+ #### Or:
1071
+ ``` python
1072
+ >> > import subprocess
1073
+ >> > a = subprocess.run([' ls' , ' -a' ], stdout = subprocess.PIPE )
1074
+ >> > a.stdout
1075
+ b ' .\n ..\n file1.txt\n file2.txt\n '
1076
+ >> > a.returncode
1077
+ 0
1078
+ ```
1079
+
1080
+
1081
+ Recursion Limit
1082
+ ---------------
1083
+ ``` python
1084
+ >> > import sys
1085
+ >> > sys.getrecursionlimit()
1086
+ 1000
1087
+ >> > sys.setrecursionlimit(5000 )
1088
+ ```
1089
+
1090
+
1083
1091
JSON
1084
1092
----
1085
1093
``` python
0 commit comments