System Programming With Python in Linux
System Programming With Python in Linux
Python in Linux
|System Programming
System Programming with Python in Linux
statements.
○ Which are intended to initialize the module.
○ Only executed the first time the module
name is encountered in an import statement.
|Importing Python Modules
| Modules are imported using the
import command.
files:
#!/usr/bin/python3
Python Modules
|Python Modules & Importing
|Checking if a Module is Imported
of that script.
| This can be used to ensure certain code only
runs when the script is being executed
directly (often for testing purposes), not when
it’s imported as a module.
Python Modules
|Checking if a Module is Imported
os Module
|os Module
| The os module in Python provides a
way of using operating system
dependent functionality.
variable management.
|File Operations
| The os module allows us to interact
with the file system, perform file
operations such as creating, reading,
writing, renaming, and deleting files.
os Module
|open()
| open() is used to open files
○ You do not need to import the os
module to use open().
○ If the file does not exist, it will
create the file.
○ There are multiple modes:
■ r - read
os Module
■ w - write
■ a - append
■ b - binary
|os.remove()
| os.remove() is used to delete a file.
○ You must to import the os module
to use os.remove().
os Module
os Module
|open() and os.remove()
|os.rename()
| os.rename() is used to rename a file
or directory.
○ It takes two arguments:
■ the current filename
■ the new filename
os Module
os Module
|os.rename()
|os.stat()
| os.stat() is used to retrieve
information about the specified file.
○ The information includes file size,
time of modification, and more.
os Module
os Module
|os.stat()
|Directory Operations
| The os module also allows us to
interact with directories in the file
system.
| Environment variables.
os Module
|environ
| os.environ returns a dictionary
containing all of the environment
variables.
os Module
|environ.get()
| os.environ.get() returns the value of
a specific environment variable.
os Module
os Module
|environ and environ.get()
|Process Management
| We can manage processes with the
os module. It gives us the capability
to fork child processes, change the
user of a process, and more.
os Module
|os.fork()
| This is often used when you want a
process to run different code
concurrently.
| For example:
○ The parent process might want to
wait for the child process to
os Module
(PPID).
os Module
|os.fork() with getuid()
● __main__
○ https://docs.python.org/3/library/__main__.html
● os Module
○ https://docs.python.org/3/library/os.html
● sys Module
○ https://docs.python.org/3/library/sys.html
● subprocess Module
○ https://docs.python.org/3/library/subprocess.html
Summary
● threading Module
○ https://docs.python.org/3/library/threading.html
● multiprocessing Module
○ https://docs.python.org/3/library/multiprocessing.html