Skip to content

Latest commit

 

History

History
95 lines (56 loc) · 1.86 KB

File metadata and controls

95 lines (56 loc) · 1.86 KB

Python Language Overview

The os Module

Reference: https://docs.python.org/3/library/os.html.

Use the os module perform command-line-style file and directory operations, and to access system environment variables.

Directory Operations

Get current working directory:

os.getcwd() # for scripts, reflects the dir where the command is being run

Get directory of current file:

os.path.dirname(__file__)) # for scripts, reflects the dir where the script file exists

Change directory:

os.chdir("/path/to/Desktop")

Make a new directory:

os.mkdir("/path/to/Desktop/my-dir")

List all files in a given directory:

os.listdir("/path/to/Desktop")

File Operations

Detect whether a specific file exists:

os.path.isfile("/path/to/Desktop/some_file.txt") #> returns True or False

Compile file paths by joining a directory with a relative file path:

os.path.join(os.path.dirname(__file__), "../../some_upstream_file.txt")

Accessing Environment Variables

To setup the following examples, follow the Environment Variables Overview and/or the dotenv package notes to set an environment variable named NYU_INFO_2335.

Get the entire environment:

import os

my_env = os.environ

print("------------")
print(type(my_env)) #> <class 'os._Environ'>
print(my_env)

print("------------")
print(type(dict(my_env))) #> <class 'dict'>
print(dict(my_env)) #> 'dict'

Get a specific environment variable (e.g. NYU_INFO_2335, only after you have set it):

# using a dictionary-like approach:
my_var = os.environ["NYU_INFO_2335"]
print(my_var) #> SecretPassword123

# using a getter function approach:
my_var = os.environ.get("NYU_INFO_2335")
print(my_var) #> SecretPassword123