Last active
August 29, 2015 14:05
-
-
Save Liudx1985/7e6b613ee180e04f5d2c to your computer and use it in GitHub Desktop.
work file-directory ,clear or zip files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:UTF-8 -*- | |
#!/usr/bin/env python | |
''' | |
Clear the directory using filter | |
''' | |
import os.path | |
import sys,os | |
import stat | |
import re | |
from xml.etree.ElementTree import * | |
skip_folders = ['.svn'] | |
class Scan_folder_recurse: | |
def __init__(self, path, pattern): | |
''' | |
path: the file path to scan & clean | |
pattern: the regex match string | |
''' | |
self.files = [] | |
self.match_pattern = re.compile(pattern, re.IGNORECASE) # use re.IGNORECASE) to judge the file ext. | |
self.__walk(path, self.__ScanFolder, self.__ScanFile) | |
''' | |
path: current dir. | |
od : function (parent_folder, file_name) | |
of : function (parent_folder, sub_folder_name); | |
''' | |
def __walk(self, path, od, of): | |
for item in os.listdir(path): | |
subpath = os.path.join(path, item) | |
mode = os.stat(subpath)[stat.ST_MODE] # get mode of file | |
if stat.S_ISDIR(mode): # folder | |
if item not in skip_folders: | |
od(path, item) | |
self.__walk(subpath, od, of) | |
elif self.match_pattern.search(item, 0, len(item)): | |
of(path, item) | |
def __ScanFolder(self, path, folder): | |
pass | |
#print("scan folder:", os.path.join(path, folder)); | |
def __ScanFile(self, path, file): | |
abs_path = os.path.join(path, file) | |
try: | |
os.remove(abs_path) | |
self.files.append(abs_path) | |
print('removed:', abs_path); | |
except IOError as e: | |
print ("Error!", e, abs_path) | |
if __name__ == '__main__': | |
scan_r = Scan_folder_recurse("D:/workspace", "(\w+)\.(ncb|pch|o|opt|exp|obj|ilk|mdmp|exception|idb|dep)$") | |
print("total files removed:", len(scan_r.files)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding :UTF-8 -*- | |
#!/usr/bin/env python | |
# todo : Give a list ,zip them into a gz file with directory level. | |
''' test of the gzip''' | |
import gzip | |
def test_zip_text(): | |
# file example ; | |
print('file example ;') | |
text = b'abbcccdddd.' | |
with open('d:/text.gz', 'wb') as fzip: | |
fzip.write(text) | |
with open('d:/text.gz', 'rb') as fzip: | |
for line in fzip: | |
print(line); | |
def test_zip_data(): | |
# text compress example: | |
print('\n\ntext compress example:') | |
text_zipped = gzip.compress(text); | |
print(text_zipped); | |
a = gzip.decompress(text_zipped); | |
print(a) | |
# test zip folder | |
from contextlib import closing | |
from zipfile import ZipFile, ZIP_DEFLATED | |
import os | |
def zip_file(filename, archivename): | |
assert os.path.isfile(filename) | |
with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z: | |
z.write(filename) | |
''' | |
@archivename :the .gz file | |
@member_file: the file name in .gz file | |
@destPath : the extract file path. | |
@psw :the password. | |
''' | |
def unzip_file(archivename, member_file, destPath = None, psw = None): | |
#with open(file, 'w') as f: | |
with closing(ZipFile(archivename, "r", ZIP_DEFLATED)) as z: | |
z.extract(member_file, destPath, psw) | |
def zip_folder_recurse(basedir, archivename, filter = None): | |
''' | |
zip files under folder @basedir into .zip file @archivename | |
if filter not None, then only files that fit filter will be zipped, others will be ignored. | |
''' | |
assert os.path.isdir(basedir) | |
with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z: | |
for root, dirs, files in os.walk(basedir): | |
#NOTE: ignore empty directories | |
for fn in files: | |
if filter and not filter(fn): | |
continue | |
absfn = os.path.join(root, fn) | |
zfn = absfn[len(basedir)+len(os.sep):] #XXX: relative path | |
z.write(absfn, zfn) | |
''' | |
@archivename :the .gz file | |
@members: the files subset of the namelist in .gz file | |
@destPath : the extract file path. | |
@psw :the password. | |
''' | |
def unzip_all(archivename, destPath = None, members=None, psw = None): | |
#with open(file, 'w') as f: | |
with closing(ZipFile(archivename, "r", ZIP_DEFLATED)) as z: | |
z.extractall(destPath, members, psw) | |
def Test_zipFolder(): | |
basedir = "D:/WorkSpace/Dxh" | |
archivename = "d:/dxh.zip" | |
file_list = { | |
"filediraux.h", | |
"flags.h", | |
} | |
filter_file_list = lambda fn : fn in file_list | |
#filter using regex to match the file ext. | |
import re | |
match_pattern = re.compile(r"(\w+)\.(c|cpp|cc|cxx|h|hpp|hxx|inc|inl)$", | |
re.IGNORECASE) # all C/C++ file | |
filter_file_ext = lambda fn : not match_pattern.search(fn) | |
zip_folder_recurse(basedir, archivename, filter_file_ext) | |
if __name__ == '__main__': | |
#zip_file('D:/movies.xml', 'd:/movies.xml.gz') | |
#unzip_file('d:/movies.xml.gz', 'movies.xml', "d:/") | |
Test_zipFolder(); | |
unzip_all('d:/dxh.zip', "d:/dxh") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment