liveBook · Manning

Os

As explained in Learn AI-Assisted Python Programming, Second Edition

os is a Python standard-library module providing an interface to operating-system services, including filesystem operations.

os is Python’s built-in interface for practical operating-system tasks. It helps programs inspect directories, build filesystem paths, check whether files or folders exist, and create output directories. section 9.5.3 section 9.4.2

Common operations include:

  • Use os.listdir(directory) to retrieve the names of files in a directory. section 9.5.3
  • Use os.path.join(directory, filename) to construct paths for reading, writing, or comparing files. section 9.4.2 section 9.5.3
  • Use os.path.exists(path) to test whether a filename or directory already exists. section 9.5.3 section 9.4.2
  • Use os.mkdir(directory) to create a missing output directory. section 9.4.2

Combining picture directories

A duplicate-free picture merger loops through source directories with os.listdir, forms each source path with os.path.join, and compares files before copying them. A filename helper uses os.path.exists to append underscores when a target name is already present. Keeping original directories intact and testing with disposable samples reduces the risk of destructive mistakes. section 9.5.3

Listing 9.5 Improved function to add covers to .pdf documents
import os
import fitz 
 
def add_covers(report_dir, cover_dir, final_dir):
    '''
    report_dir is a directory of reports named 1.pdf, 2.pdf, and so on.
    These files may be one page or more than one page.
 
    cover_dir is a directory of covers, with one cover per report.
    The filenames in this directory are cover1.pdf, cover2.pdf, and so on.
    Each of these files is one page.
 
    Add the cover to the beginning of each report,
    and store all resulting pdfs in final_dir.
    '''
    if not os.path.exists(final_dir):          #1
        os.mkdir(final_dir)                         #2
    for report in os.listdir(report_dir):             #3
        report_path = os.path.join(report_dir, report)
        cover_path = os.path.join(cover_dir, f'cover{report}')
        final_path = os.path.join(final_dir, report)
        cover_doc = fitz.open(cover_path)                   #4
        report_doc = fitz.open(report_path)       #5
        final_doc = fitz.open()                #6
        final_doc.insert_pdf(cover_doc)       #7
        final_doc.insert_pdf(report_doc)       #8
        final_doc.save(final_path)            #9

if __name__ == '__main__':
    add_covers('reports', 'covers', 'final')

Browse a complete PDF-cover workflow using os.path.exists, os.mkdir, os.listdir, and os.path.join alongside a document-processing library. section 9.4.2

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Demonstrates os.listdir, os.path.join, os.path.exists, and os.mkdir in task-focused functions without exploring operating-system internals.
Examples
Uses PDF cover insertion, duplicate-free picture merging, collision-free filenames, and disposable test directories.
What it assumes
Teaches filesystem operations through imports, functions, loops, and concrete file-processing tasks rather than requiring external os installation.
What you can do after
Apply it via Python to inspect directories, construct paths, detect existing files, and prepare output folders.
How it connects
Connects filesystem paths and directory traversal with shutil, filecmp, PyPDF2, and PyMuPDF.

Also explained in

Compare how 5 books explain os →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.