Daily Reading 19
Automation
###
How can you use regular expressions in Python to search for specific patterns in a string, and what is the primary module to work with them?
Regular expressions, or “regex” are in some ways, a smaller programming language that allows you to specify combinations of ASCII characters to find within larger strings. Python has built in support for regex, but it must be enabled via import re
.
Regex has variables, is able to look for singular or sets of characters, nested grouping, and much more, though it can be unwieldy to learn at first.
What is the purpose of the shutil module in Python, and provide an example of a common use case for file or directory management with this module?
Shutil is a python module used for handling directory and filesystem management. Copying files or directories, migrating files or directories from one location to another, or compressing/archiving files into .tar or .zip are all possible through it.
A common use case for this module would be it’s size analysis function, where you can see which files and folders are taking up the most space on your system, in order to figure out which ones should be archived, deleted, or otherwise “reduced” in some way.
Compare and contrast the os and shutil modules. When would you choose to use one over the other?
os module has a larger and broader scope of actions it can perform, for example direct file modification and editing, change what user is the “owner” of a process, wait until another child process is completed before running, and other highly detailed OS system management level actions.
Shutil is suited for basic file management. os is suited for highly precise operating system actions.