我提出了这个问题,但是如何为传统的python shell进行实质性的缩短呢。
import zlib, sys, time, base64, pymsgbox, ttk
from tkFileDialog import askopenfilename
import Tkinter as tk
def getPath():
path = askopenfilename(filetypes=[("Text Files", (".txt")), ("Text", '.txt'), ('All', '*')], )
return path
def compress():
text = open(getPath(), 'r').read()
realSize = 'Raw size :' + str(sys.getsizeof(text))
print(realSize)
comperesed = base64.b64encode(zlib.compress(text, 9))
compressedSize = 'Raw size :' + str(sys.getsizeof(comperesed))
print(compressedSize)
compressedFile = pymsgbox.prompt('Compressd File Name:') + '.txt'
encoded = open(compressedFile, 'a')
encoded.write(comperesed)
encoded.close()
def decompress():
readFile = open(getPath(), 'r').read()
decompressed = zlib.decompress(base64.b64decode(readFile))
decompressedFile = pymsgbox.prompt('Decompressd File Name:') + '.txt'
decoded = open(decompressedFile, 'a')
decoded.write(decompressed)
decoded.close()
def gui():
# Make Message
pymsgbox.alert('Welcome', 'Compressing and Decompressing')
win = tk.Tk()
win.title("Compressing and Decompressing")
aLabel = ttk.Label(win, text="Compress")
aLabel.grid(column=0, row=0)
aLabel = ttk.Label(win, text="Decompress")
aLabel.grid(column=0, row=1)
action = ttk.Button(win, text="Convert", command=compress)
action.grid(column=1, row=0)
action = ttk.Button(win, text="Convert", command=decompress)
action.grid(column=1, row=1)
win.mainloop()
gui()
答案 0 :(得分:0)
如果我没有正确地解决问题,你只想从python shell运行压缩和解压缩函数。
import zlib, sys, base64
def compress(decompressedFile, compressedFile):
text = open(decompressedFile, 'r').read()
realSize = 'Raw size :' + str(sys.getsizeof(text))
print(realSize)
comperesed = base64.b64encode(zlib.compress(text, 9))
compressedSize = 'Raw size :' + str(sys.getsizeof(comperesed))
print(compressedSize)
encoded = open(compressedFile, 'a')
encoded.write(comperesed)
encoded.close()
def decompress(compressedFile, decompressedFile):
readFile = open(compressedFile, 'r').read()
decompressed = zlib.decompress(base64.b64decode(readFile))
decoded = open(decompressedFile, 'a')
decoded.write(decompressed)
decoded.close()
我将此文件保存为test554.py并使用:
>>> import test554
>>> test554.compress("testuncompress.txt", "testuncompress2.txt")
Raw size :115
Raw size :129
>>> test554.decompress("testuncompress2.txt", "testuncompress3.txt")
首先将testuncompress.txt文件压缩为testuncompress2.txt,然后将testuncompress2.txt解压缩为testuncompress3.txt。