Lecture 19

The os module

MCS 260 Fall 2020
Emily Dumas

Reminders

  • Project 2 due Friday at 6:00pm central
  • Worksheet 7 available
  • The os module

    
            import os
        

    gives your program access to lots of functions that ask the operating system to do things.

    Today we focus on the filesystem operations in this module.

    Live coding

    Shell commands

    • pwd - print working directory
    • cd - change working directory
    • ls - list files in a directory
    • cat - display file contents
    • rm - remove a file
    • mkdir - create a directory
    • rmdir - remove a directory

    os module methods

    • os.getcwd() - get working directory
    • os.chdir(dirname) - change working directory
    • os.listdir(dirname) - get list of files in directory
    • os.mkdir(dirname) - create a directory
    • os.rmdir(dirname) - remove a directory
    • os.remove(filename) - remove a file

    os module methods

    • os.path.join(part0,part1,...) - join path components (using proper separator for the OS)
    • os.path.exists(name) - True if name exists
    • os.path.isfile(name) - True if name is a file
    • os.path.isdir(name) - True if name is a directory

    References

    Revision history

    • 2020-10-06 Initial publication