Modules
|
Modules are simply Python files.
To use code from a module, it has to be imported.
The import command is used to import code from a module.
There are different ways to import from a module. Select the appropriate method based on the required effect.
|
Working with Files
|
Files are opened with the open() function.
The open() function returns a file object.
Files need to be closed when they are no longer required by your program.
The close() method of the file object closes a file.
The with statement provides an elegant and safe way to automatically close your files.
Data can be read from text files a line at a time by the readline() file object method.
If you want to process each line of a text file in order, then iteration is useful.
Data can be written to a file with the write() file object method.
Patterns are common and repeatable recipes for common problems. The text file sequential processing pattern is simple, robust and efficient.
|
file processing
|
|
Creating and Running Python Scripts
|
Python scripts are plain text files, usually with the .py extension.
You execute Python scripts at the command-line with the Python interpreter.
Values are displayed using the print function.
Comments are indicated with # . Anything between the # and the end of the line is ignored.
|
Getting arguments from the command-line
|
|
What's in a __name__?
|
__name__ is a string containing the module name.
For files directly executed by Python, __name__ gets the special value “main”, indicating that this is the main file.
The if __name__ == "__main__" expression prevents code from running when the module is being imported.
|
Final practical: How Many Articles Reference that they use R and/or Python?
|
|