Main file
1 | |
Reading files
Reads file as a string:
1 | |
Reads file line by line:
1 | |
String operations
Remove spaces at the beginning and at the end of the string:
1 | |
Split strings:
1 | |
1 | |
Join:
1 | |
File path operations
1 | |
Makes a new directory if it does not exist:
1 | |
In order to be able to import modules from packages in the parent directory:
1 | |
Gets a list of all base names (does not distinguish subdirectories and files) in the current directory:
1 | |
Gets a list of all subdirectories in the current directory:
1 | |
Generates the file names in a directory tree by walking the tree. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple. dir_path is a string, the path to the directory. dir_names is a list of the names of the subdirectories in dir_path (excluding . and ..). file_names is a list of the names of the non-directory files in dir_path.
1 | |
Uses glob to find all the path names matching a specified pattern according to the rules used by the Unix shell:
1 | |
Pickle
1 | |
Inheritance
1 | |
Reimports a module in python while interactive
1 | |
Exceptions
1 | |
1 | |
1 | |
1 | |
1 | |
Gets object attributes in Python
1 | |
Sorting
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
Dict comprehension to reverse key-value pair in a dictionary
1 | |
Built-in functions
globals()current global symbol table. It always returns the dictionary of the module namespace. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).locals()current local symbol table. It always returns a dictionary of the current namespace.
Parsing arguments
1 | |
For nargs:
N(an integer).Narguments from the command line will be gathered together into a list.?One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value fromdefaultwill be produced. Note that for optional arguments, there is an additional case - the option string is present but not followed by a command-line argument. In this case the value fromconstwill be produced.*All command-line arguments present are gathered into a list.+Just like*, all command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn’t at least one command-line argument present.