Search code examples
pythonpicklesoftware-distribution

trying to store a file in a string python


This is one of my first python projects, and i'm trying to make a script that would write a script which can re-create the src/ directory. this would be what i distribute to users. It uses walk, and writes a python file that first creates all the directories, and then writes the files. The issue i have is making the the files into a single string that i can write to a file.

This is the program i have:

import os
import pickle

src = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'src'
fPack = 'import os \nimport pickle \nmyDir = os.path.dirname(os.path.realpath(__file__))'
Pack =''
print 'Packing ' + src
pickle
for root, dirs, files in os.walk(src, topdown=True):
    for name in files:
        print os.path.join(root, name)
        f = open(os.path.join(root, name), 'r')
        Pack = Pack + '\nf = open(os.path.join(myDir,\'' + name + '\'), \'w\')'
        fileCont = pickle.dumps(f.read())
        Pack = Pack + '\nf.write(pickle.loads(\'' + fileCont + '\'))'
    for name in dirs:
        print os.path.join(root, name)
        fPack = fPack + '\nos.makedirs(os.path.join(myDir,\'' + name + '\'))'

print '==================================================\n\n\n'
print fPack + Pack 
f = open(os.getcwd() + os.sep + 'dist' + os.sep + 'Pack.py', 'w')
f.write(fPack)
f.write(Pack)

And if i run it in a directory with on subdirectory, and on file inside it creates this file

import os 
import pickle 
myDir = os.path.dirname(os.path.realpath(__file__))
os.makedirs(os.path.join(myDir,'SphereText'))
f = open(os.path.join(myDir,'TextMain.py'), 'w')
f.write(pickle.loads('S"########################################################\n#Main SphereText file.                                 #\n#SpereText is a simple Notepad-Like plain text editor  #\n########################################################\n\nfrom Tkinter import *\nfrom tkFileDialog import *\nimport tkSimpleDialog\n\nroot = Tk()\nroot.title('SphereText')\n\ndef fSave():\n    fileName = asksaveasfilename(parent=root)\n    f = open(fileName, 'w')\n    f.write(text.get(1.0,END))\n\ndef fOpen():\n    fileName = ''\n    fileName = askopenfilename(parent=root)\n    f = open(fileName, 'r')\n    text.delete(1.0,END)\n    text.insert(1.0, f.read())\n\ndef tReplace():\n        Old = tkSimpleDialog.askstring('SphereText', 'Replace:')\n        print Old\n        New = tkSimpleDialog.askstring('SphereText', 'With:')\n        print New\n        content = text.get(1.0,END)\n        content = content.replace(Old, New)\n        text.delete(1.0,END)\n        text.insert(1.0, content)\n    \nmenubar = Menu(root)\nmenubar.add_command(label='Save', command=fSave)\nmenubar.add_command(label='Open', command=fOpen)\nmenubar.add_command(label='Replace', command=tReplace)\nroot.config(menu=menubar)\n\ntext = Text(root, wrap=WORD)\n\ntext.pack()\n\nroot.mainloop()\n"
p0
.'))

The 's aren't escaped, and there are two line breaks at the end. i thought that the whole point of serializing was that you could always read it back the same way. Anyone know how i can mak the file a valid string?


Solution

  • Sorry about the newbish question, i just found out i had been trying to reinvent the wheel. apparently, that already exists under the name Squeeze.