#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import os.path
import re
def moveFile(infile, outfile):
inBuffer = ""
with open(infile, "rb+") as fr:
inBuffer = fr.read()
with open(outfile, "wb+") as fw:
fw.write(inBuffer)
# 压缩
def zipFile(infile, outfile):
inBuffer = ''
outBuffer = ''
with open(infile, 'rb+') as fr:
inBuffer = fr.read()
outBuffer = inBuffer.replace("\r\n", '')
outBuffer = re.sub(re.compile(r'\{\s+'), '{', outBuffer)
outBuffer = re.sub(re.compile(r'\[\s+'), '[', outBuffer)
outBuffer = re.sub(re.compile(r'\s+\}'), '}', outBuffer)
outBuffer = re.sub(re.compile(r',\s+'), ',', outBuffer)
outBuffer = re.sub(re.compile(r':\s'), ':', outBuffer)
with open(outfile,'wb+') as fw:
fw.write(outBuffer)
print infile, 'zip done '
# 压缩路径下文件
def encodeDir(rootdir, outDir):
for parent,dirnames,filenames in os.walk(rootdir):
if len(filenames) > 0:
for filename in filenames:
files = filename.partition(".")
inPath=os.path.join(parent,filename)
outDir2 = outDir+parent.replace(rootdir, "")
if not os.path.exists(outDir2):
os.makedirs(outDir2)
outPath=os.path.join(outDir2, filename)
if len(files) > 2:
if files[len(files)-1] == "json":
zipFile(inPath, outPath)
else:
moveFile(inPath, outPath)
if len(sys.argv) != 3:
if len(sys.argv) < 2:
print('please input import folder ')
elif len(sys.argv) < 3:
print('please input export folder ')
else:
encodeDir(sys.argv[1], sys.argv[2])
python27 压缩 json
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、背景 最近刚刚做完一个中文汉字笔画排序的功能,链接如下: 【我的Android进阶之旅】Android实现中文...
- 年初我还在写Java,一转头,已经写了一段时间的Python了 :)。 让程序员吵起来的话:“PHP是最好的语...
- 本文主要介绍Huffman编码、Huffman树、和如何借助Python实现Huffman编码树对文件进行压缩和解...
- 一、序言 使用Nginx作为web应用服务时,会代理如下常见文件:js、css、JSON、图片等,本文提供基于Ng...
- 压缩包也是我们平时工作中经常要接触到的文件格式,压缩文件后缀名通常有 .zip、.rar、.7z 等等。Pytho...