A file containing Python definitions and statements is known as a Python module. Functions, classes, and variables are all defined by a module. Runnable code is another option for a module. It's easier to comprehend and use code when it's organised into modules. It also helps to arrange the code.
You can use modules to structure your Python code sensibly. It's easier to comprehend and use code when it's organised into modules. A module is a Python object that may be bound and referenced and has characteristics with random names.
To use the module's capabilities, we must first load it into our Python code. Python has two sorts of statements, which are described here:
1. The import statement
The import statement is used to copy all of a module's functionality to another. It's worth noting that any python source file can be used as a module in another python source file.
We can import numerous modules with a single import line; however, a module is only loaded once, regardless of how many times it has been imported into our file.
2. The from-import statement
Instead of importing the whole module into the namespace, Python provides the flexibility to import only the specific attributes. This can be done by using an import statement.
If we know the characteristics to be imported from the module, we should always use the from-import statement. It prevents our code from becoming bulkier. We can also use * to import all of the attributes from a module.
Variables' scope
Variables are connected with two types of scopes in Python. Unless and until defined within a function, all variables defined in a module have global scope.
All variables defined within a function have a local scope that is confined to the function's scope. We can't use a global variable to access a local variable.
If two variables with the same name are defined with two distinct scopes, i.e., local and global, the local variable will always take precedence.
Packages for Python
Python packages provide a hierarchical directory structure in which a package contains sub-packages, modules, and sub-modules, making it easier for developers to work with the application development environment. The packages are used to organise the application-level code neatly.
Python Files
Python offers file handling and allows users to act on files via reading and writing files and many additional file handling options. The concept of file handling has been extended to various other languages, but the implementation is either complicated or lengthy. However, like most Python principles, this concept is simple.
Python processes file differently depending on whether they are text or binary, which is crucial. Each line of code consists of a series of characters that together constitute a text file.
A specific character called the EOL or End of Line character, such as the comma, or a newline character, is used to end each line in a file. It signals to the interpreter that the current line has ended and that a new one has begun.
Printing to the Screen
The print statement, which accepts zero or more expressions separated by commas, is the simplest way to generate output. This function turns your expressions into a string and outputs the result to standard output.
Input From the Keyboard
Python has two built-in routines for reading a line of text from standard input, which is usually the keyboard. These are the functions:
- raw_input
- input
Opening and Closing Files
By default, Python provides the essential functions and methods needed to manipulate files. The majority of file manipulation can be done with a file object.
The open Function
You must first open a file with Python's built-in open() function before you can read or write to it. This function creates a file object that can be used to invoke other file-related support functions.
The file Object Attributes
You may retrieve numerous information about a file after it is opened and you have one file object.
The following is a list of all characteristics associated with the file object:
The close() Method
A file object's close() method flushes any unwritten data and closes the file object, which means no additional writing can be done.
When a file's reference object is reassigned to another file, Python immediately shuts it. When closing a file, it's best to use the close() method.
Reading and Writing Files
To make our life easier, the file object provides a set of access methods. We'll look at how to read and write files using the read() and write() functions.
The write() Method
Any string can be written to an open file using the write() method. It's worth noting that Python strings can contain binary data as well as text.
The string is not terminated with a newline character ('n') when using the write() method.
The read() Method
A string is read from an open file using the read() method. It's worth noting that Python strings can contain binary data. in addition to text data
Positions of Files
The inform() method returns the current file position; in other words, the next read or write will occur at that many bytes from the file's beginning.
The seek(offset[, from]) method is used to modify the file's current position. The number of bytes to be relocated is specified by the offset argument. The from option indicates the starting point for the bytes to be relocated from.
If from is set to 0, the file's beginning will be used as the reference position, 1 the current position will be used as the reference position, and 2 the file's end will be used as the reference position.
Renaming and Deleting Files
The OS module in Python provides methods for performing file-processing activities, including renaming and removing files.
To utilise this module, you must first import it before calling any related functions.
The rename() Method
The rename() method takes two arguments, the current filename and the new filename.
The remove() Method
You can use the remove() method to delete files by supplying the file's name to be deleted as the argument.