This is a draft cheat sheet. It is a work in progress and is not finished yet.
import re
re.findall()
re.finditer()
re.sub()
re.compile()
MatchObject
m.group()
|
Directory operation
import os
import pathlib
# get containing dir name
os.path.dirname(path)
# create directory recursively
os.makedirs(path, exist_ok=True)
path = pathlib.Path(path)
path.mkdir(parents=True)
|
|
|
File Operation - op.path
import os
import shutil
import glob
import subprocess
os.getcwd()
os.chdir(path)
os.mkdir(dir)
os.makedirs(dir)
os.rmdir(dir)
shutil.rmtree(dir)
os.remove() # file
# return list of File objects
os.listdir(path)
glob.glob(path)
os.path.isdir()
os.path.isfile()
shutil.copy()
shutil.copyfile()
shutil.copy2()
shutil.copytree()
os.system('command')
subprocess.call('command')
# zip/unzip file
import gzip # for gzip file
import zipfile # for zip file
import tarfile # for gzip tar file
shutil.make_archive('files','zip',path)
# file
f = open('file.py','w')
f.close()
f.readline()
f.readlines(n)
# mode:'r', 'w', 'a', 'rb', 'wb', 'ab'
|
|
|
|