Coding in Python 18 - The OS Module

From LearnLinux.tv Wiki
Jump to navigationJump to search

Overview

In video number 18 of the Coding in Python series, we'll check out the OS module, which will allow you to interact with your operating system.

Relevant Links
Original Video


Commands Used in this Video

Using the os module to print the current working directory

#!/usr/bin/env python3

import os

print(os.getcwd())

Renaming a file

#!/usr/bin/env python3

import os

print(os.getcwd())

os.rename("first.txt", "second.txt")

Running a system command

#!/usr/bin/env python3

import os

os.system('ls')

Using the os module with print statements

#!/usr/bin/env python3

import os

print("Your current working directory is: " +os.getcwd() + "\n\n")

print("The contents of this directory are:")
os.system('ls')