Module 7 - Modules and File handling
Module 7 - Modules and File handling
Handling
PPE - Module 7
In this module, you will learn about:
a file containing Python definitions and statements, which can be later imported
and used when necessary.
Scenario:
We develop a program that uses the same functions throughout the
whole program.
Solution: module, so the program can use the functions defined in the module
once instead of defining the same function multiple times throughout the program.
Importing and using Python modules
Trigonometry
exponentiation
general-purpose functions
Note the prefix pseudo - the numbers generated by the modules may look random
in the sense that you cannot predict their subsequent values, but don't forget that
they all are calculated using very refined algorithms.
● seed()
● random()
● randrange()
● shuffle()
● choice()
● choices()
● sample()
More on Python modules
● https://docs.python.org/3/py-modindex.html
Simulation [21]
Problem Set [18]
Python Modules and Package
Writing your own modules doesn't differ much from writing ordinary scripts.
There are some specific aspects you must be aware of, but it definitely isn't rocket science. You'll see this soon
enough.
● a module is a kind of container filled with functions - you can pack as many functions as you want into one
module and distribute it across the world;
● of course, it's generally a good idea not to mix functions with different application areas within one module
(just like in a library - nobody expects scientific works to be put among comic books), so group your functions
carefully and name the module containing them in a clear and intuitive way (e.g., don't give the name
arcade_games to a module containing functions intended to partition and format hard disks)
● making many modules may cause a little mess - sooner or later you'll want to group your modules exactly in
the same way as you've previously grouped functions - is there a more general container than a module?
● yes, there is - it's a package; in the world of modules, a package plays a similar role to a folder/directory in
the world of files.
Simulation [22]
Python Modules and Package
Pypi.org
Modes :
the file associated with the stream must exist and has to be readable, otherwise the
open() function raises an exception.
the file associated with the stream doesn't need to exist; if it doesn't exist it will be
created; if it exists, it will be truncated to the length of zero (erased); if the creation
isn't possible (e.g., due to system permissions) the open() function raises an exception.
File Handling(cont.)
the file associated with the stream doesn't need to exist; if it doesn't exist, it will be created; if it exists the
virtual recording head will be set at the end of the file (the previous content of the file remains untouched.)
the file associated with the stream must exist and has to be writable, otherwise the open() function raises
an exception;
both read and write operations are allowed for the stream.
the file associated with the stream doesn't need to exist; if it doesn't exist, it will be created; the previous
content of the file remains untouched;
both read and write operations are allowed for the stream.
File Handling(cont.)