Cheatography
https://cheatography.com
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.randint(a,b) |
generate random number N a<=N<=b |
random.shuffle(list) |
shuffles elements of a list |
sys
sys.argv |
argument list |
sys.exit() |
terminate program |
File manipulation
obj=open('path','mode') |
opens a file and returns a file object |
with open('file',mode) as obj |
same as open but closes file automatically |
obj.readlines() |
read lines of a text file |
obj.write('string') |
writes string to text file |
obj.close() |
closes obj file object |
obj.shelve.open('filename') |
open a persistent dictionary |
modes: 'r','w','a','rb','wb'
copy
copy.copy(obj) |
makes copy of mutable obj (list or dict) |
copy.deepcopy(obj) |
makes a copy of objs inside obj recursively |
pprint (pretty print)
pprint.pprint(obj) |
prints the formatted object on stream |
pprint.pformat(obj) |
Returns the formatted object as a string |
send2trash
send2trash.send2trash('file') |
sends file to recycle bin |
zipfile
obj=zipfile.zipfile('file.zip') |
opens file obj to compress file.zip |
obj.write('file.txt',compress_type=zipfile.ZIP_DEFLATED) |
obj.extractall('file.zip') |
extracts files from zip file |
obj.close() |
closes compress and extract file obj |
obj.namelist() |
return a list of archive members by name |
info=obj.getinfo('file.zip') |
Return info about the archived file |
info.file_size |
returns original file size in bytes |
info.compress_size |
returns the compressed file size |
|
|
os
os.chdir('path') |
change firectory |
os.getcwd() |
get current working dir |
os.listdir('path') |
returns list of filename strings in 'path' |
os.makedirs('path') |
recursive directory creation |
os.rmdir('path') |
deletes the directory path if empty |
os.path.abspath('path') |
returns string w/absolute path |
os.path.basename('path') |
returns the filename of the path |
os.path.dirname('path') |
returns the directory name of pathname path |
os.path.exists('path') |
returns True if path refers to existing path |
os.path.getsize('path') |
returns filesize in bytes of the file in path |
os.path.isabs('path') |
returns True if path is absolute |
os.path.isdir('path') |
returns True if path is an existing directory |
os.path.isfile('path') |
returns True if path is an existing file |
os.path.join() |
join strings into a file path |
os.path.relpath('path') |
returns relative path string |
os.path.split('path') |
returns a tuple w/path separated from basename |
os.unlink('path') |
delete file at path (irreversible) |
os.walk('path') |
returns list of strings for path,subdirs,files vars |
Path format in windows - 'Drive:\\Dir'
pyperclip (copies data to clipboard)
pyperclip.copy(obj) |
copies obj to clipboard |
pyperclip.paste() |
pastes clipboard content |
requests
r=requests.get('url') |
downloads url contents |
r.raise_for_status() |
raise exception if download fails |
r.status.code() |
returns request status code |
r.headers['content-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 redirection 302 if redirected |
r.url |
returns requested url |
r.iter_content(N) |
returns "chunks of N bytes" per iteration |
requests.post('url',data={'key':'value'}) |
pos request |
|