Skip to content

Instantly share code, notes, and snippets.

@chaomai
chaomai / copy_to_target_location_2.py
Created September 8, 2012 09:19
Python 3: copy to target location 2
#scan a directory iteratively, copy particular file to a new directory
#each file in new directory has is own new directory that name is same as the file
#tested under Python3
import os
import os.path
import shutil
def copy_file(item, sou_dir, des_dir, type):
(filepath, filename) = os.path.split(item)
if type == os.path.splitext(filename)[1]:
@chaomai
chaomai / copy_to_target_location.py
Created September 8, 2012 09:17
Python 3: copy to target location
#scan a directory iteratively, copy particular file to a new directory
#tested under Python3
import os
import os.path
import shutil
def copy_file(item, sou_dir, des_dir, type):
(filepath, filename) = os.path.split(item)
if type == os.path.splitext(filename)[1]:
sourcefile = os.path.join(sou_dir, item)
@chaomai
chaomai / detect_encoding.py
Created September 8, 2012 09:16
Python 3: detect encoding
#detect files' encoding
#tested under Python3
from chardet.universaldetector import UniversalDetector
import os
import os.path
def detect(filename, filetype, out_enc='utf-8'):
(filepath, name) = os.path.split(filename)
if filetype == os.path.splitext(name)[1]:
try:
@chaomai
chaomai / get_specific_type_files.py
Created September 8, 2012 09:15
Python 3:get specific type files
#scan a directory iteratively and get specific type files
#tested under Python3
import os
import os.path
def print_filename(item, sou_dir, type):
(filepath, filename) = os.path.split(item)
if type == os.path.splitext(filename)[1]:
sourcefile = os.path.join(sou_dir, item)
print(sourcefile)
@chaomai
chaomai / is_palindrome.py
Created September 8, 2012 09:13
Python 3: is palindrome
#palindrome
#tested under Python3
def process_text(text):
text = text.lower()
forbidden = (' ', '.', '?', '!', ':', ';', '-', '—', '(', ')', '[', ']', '’', '“', '”', '/', ',', '"')
for i in forbidden:
text = text.replace(i, '')
return text
def reverse(text):