Module3 Deep File Process Management
Module3 Deep File Process Management
Process Management
Topics: Scripting Files & Folders,
os.path, pathlib, subprocess, os.exec,
os.fork, os.kill
Scripting Files and Folders
• • Use os module to manage files/folders:
create, move, rename, delete.
• • os.makedirs(path): Create folders, including
intermediate ones.
• • os.listdir(path): List all files/folders in a
directory.
• • os.getcwd(), os.chdir(path): Manage current
working directory.
Working with os.path
• • os.path.join(): Build paths compatible with
OS.
• • os.path.exists(): Check if a file/folder exists.
• • os.path.getsize(): Get file size in bytes.
• • os.path.basename(), dirname(): Get file
name or directory path.
Working with pathlib
• • Pathlib offers an object-oriented approach to
file paths.
• • Path('filename').exists(), .is_file(), .is_dir()
• • Traversal: Path('dir').glob('**/*.py') for
recursive search.
• • Easier and cleaner syntax than os.path.
os.path vs pathlib
• • os.path is function-based; pathlib is object-
oriented.
• • pathlib is recommended for modern Python
(3.4+).
• • Both support cross-platform path handling.
• • Example: Path('folder') / 'file.txt' is easier
than os.path.join().
Introduction to Process
Management
• • OS process management is crucial for
automation.
• • Process: a running instance of a program.
• • Use Python to spawn, control, or replace
processes.
• • Modules: os.system, subprocess, os.exec,
os.fork, os.kill.
os.system()
• • Executes shell commands (e.g.,
os.system('ls')).
• • Returns exit code of the command.
• • Limited in capturing output or handling
errors.
• • Not recommended for modern scripts.
subprocess Module
• • subprocess.run(): Execute commands with
control over input/output.
• • subprocess.call(), check_output(): Additional
options for scripts.
• • Example: subprocess.run(['ls', '-l'],
capture_output=True)
• • Captures stdout, stderr; allows timeout
handling.
Why Use subprocess Over
os.system()
• • subprocess is safer and more flexible.
• • Avoids shell injection vulnerabilities.
• • Easier to work with outputs and errors.
• • Future-proof choice for command execution.
os.exec* Functions
• • Replaces the current process with a new
one.
• • os.execl(), execv(), execvp(), etc.
• • Example: os.execl('/bin/ls', 'ls', '-l')
• • No return to current script after exec.
os.fork()
• • Creates a child process (only on Unix/Linux).
• • Returns 0 in child, PID in parent.
• • Common in daemons and multiprocessing.
• • Use os.wait() to wait for child process to
finish.
os.kill()
• • os.kill(pid, signal): Sends a signal to a
process.
• • Common signals: signal.SIGTERM,
signal.SIGKILL.
• • Used to terminate or interrupt a process.
• • Must be used with care.
Summary and Best Practices
• • Use pathlib for modern file path handling.
• • Use subprocess instead of os.system.
• • Use exec, fork, and kill only for advanced
needs.
• • Always handle exceptions and process
outputs.
References
• • Python Official Docs:
https://docs.python.org/3/
• • os, os.path, pathlib modules
• • subprocess and signal modules
• • Module-3 notes by Prof. Manjusha (SVIT)