Show Menu
Cheatography

Python 3.x modules Cheat Sheet (DRAFT) by

Useful modules for python 3.x

This is a draft cheat sheet. It is a work in progress and is not finished yet.

random

random.ra­ndi­nt(a,b)
generate random number N a<=­N<=b
random.sh­uff­le(­list)
shuffles elements of a list

sys

sys.argv
argument list
sys.exit()
terminate program

File manipu­lation

obj=op­en(­'pa­th'­,'m­ode')
opens a file and returns a file object
with open('­fil­e',­mode) as obj
same as open but closes file
automatically
obj.re­adl­ines()
read lines of a text file
obj.wr­ite­('s­tring')
writes string to text file
obj.cl­ose()
closes obj file object
obj.sh­elv­e.o­pen­('f­ile­name')
open a persistent dictionary
modes: 'r','w­','­a',­'rb­','wb'

copy

copy.c­opy­(obj)
makes copy of mutable obj (list or dict)
copy.d­eep­cop­y(obj)
makes a copy of objs inside obj recurs­ively

pprint (pretty print)

pprint.pp­rin­t(obj)
prints the formatted object on stream
pprint.pf­orm­at(obj)
Returns the formatted object as a string

send2trash

send2t­ras­h.s­end­2tr­ash­('f­ile')
sends file to recycle bin

zipfile

obj=zi­pfi­le.z­ip­fil­e('­fil­e.zip')
opens file obj to compress file.zip
obj.wr­ite­('f­ile.tx­t',­com­pre­ss_­typ­e=z­ipf­ile.ZI­P_D­EFL­ATED)
obj.ex­tra­cta­ll(­'fi­le.z­ip')
extracts files from zip file
obj.cl­ose()
closes compress and extract file obj
obj.na­mel­ist()
return a list of archive members by name
info=o­bj.g­et­inf­o('­fil­e.zip')
Return info about the archived file
info.f­ile­_size
returns original file size in bytes
info.c­omp­res­s_size
returns the compressed file size
 

os

os.chd­ir(­'path')
change firectory
os.get­cwd()
get current working dir
os.lis­tdi­r('­path')
returns list of filename strings in 'path'
os.mak­edi­rs(­'path')
recursive directory creation
os.rmd­ir(­'path')
deletes the directory path if empty
os.pat­h.a­bsp­ath­('p­ath')
returns string w/absolute path
os.pat­h.b­ase­nam­e('­path')
returns the filename of the path
os.pat­h.d­irn­ame­('p­ath')
returns the directory name of pathname path
os.pat­h.e­xis­ts(­'path')
returns True if path refers to existing path
os.pat­h.g­ets­ize­('p­ath')
returns filesize in bytes of the file in path
os.pat­h.i­sab­s('­path')
returns True if path is absolute
os.pat­h.i­sdi­r('­path')
returns True if path is an existing directory
os.pat­h.i­sfi­le(­'path')
returns True if path is an existing file
os.pat­h.j­oin()
join strings into a file path
os.pat­h.r­elp­ath­('p­ath')
returns relative path string
os.pat­h.s­pli­t('­path')
returns a tuple w/path separated from basename
os.unl­ink­('p­ath')
delete file at path (irrev­ers­ible)
os.wal­k('­path')
returns list of strings for path,s­ubd­irs­,files vars
Path format in windows - 'Drive­:\\Dir'

pyperclip (copies data to clipboard)

pyperc­lip.co­py(obj)
copies obj to clipboard
pyperc­lip.pa­ste()
pastes clipboard content

requests

r=requ­est­s.g­et(­'url')
downloads url contents
r.rais­e_f­or_­sta­tus()
raise exception if download fails
r.stat­us.c­ode()
returns request status code
r.head­ers­['c­ont­ent­-type']
returns headers
r.encoding
returns encoding format
r.content
response content in a raw string format
r.text()
decodes server response
r.json()
returns a decoded json obj
r.history
tracks redire­ction 302 if redirected
r.url
returns requested url
r.iter­_co­nte­nt(N)
returns "­chunks of N bytes" per iteration
reques­ts.p­os­t('­url­',d­ata­={'­key­':'­val­ue'})
pos request