dum3ng / study-issues

0 stars 0 forks source link

python cheatsheet #29

Open dum3ng opened 4 years ago

dum3ng commented 4 years ago

file manipulation

import os

# create file
f = open('some.file', 'w+') # open a file for write, and create it if not exists
# remove
os.remove('file')
# copy
from shutil import copyfile
copyfile(src, dist)
# rename file 

path

from os import path
# join path
p = path.join('/a/b/c', '../d') # -> '/a/b/c/../d'
path.abspath(p) # -> '/a/b/d'

regex

import re

# search
pattern = re.compile('god')
pattern.search('computing god')