Os

import os

os.chdir(path) #cd
cwd = os.getcwd() #pwd

Get all files in subdirs

for root, dirs, files in os.walk(PATH_TO_NOTES):
  for file in files:
    curFile = os.path.join(root, file)

Get Environment

os.environ.get('SECRET_KEY')

Files

Underlying open is from io library

#f is file object
f = open('workfile', 'w')
f.write("Now the file has one more line!")
f.close()
  
#To write bytes
with open(filename, 'wb') as f: 
  f.write(filebytes)

Modes

  • 'r' read; DEFAULT

  • 'w' writing(erase existing file)

  • 'w+' writing(create new file if it doesn't exist)

  • 'a' appending to the end

  • 'a+' appending to the end(create new file if needed)

  • 'r+' both reading and writing

Reading Data

With

Automatically close for ya

Pickle

CPickle

  • optimized cousin cPickle written in C, 1000x faster

  • just doesn't support subclassing of the Pickler() and Unpickler() classes

  • usually better

  • same interface

Advanced OS*

  • could bypass system caches, more metadata/permissions access

Play Audio Files

Last updated