Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图以字符串形式存储文件_Python_Pickle_Software Distribution - Fatal编程技术网

Python 试图以字符串形式存储文件

Python 试图以字符串形式存储文件,python,pickle,software-distribution,Python,Pickle,Software Distribution,这是我的第一个python项目之一,我正在尝试编写一个脚本来编写一个可以重新创建src/目录的脚本。这将是我分发给用户的内容。它使用walk,并编写一个python文件,该文件首先创建所有目录,然后写入文件。我遇到的问题是将文件转换成一个字符串,我可以将其写入文件 这是我的计划: import os import pickle src = os.path.dirname(os.path.realpath(__file__)) + os.sep + 'src' fPack = 'import o

这是我的第一个python项目之一,我正在尝试编写一个脚本来编写一个可以重新创建src/目录的脚本。这将是我分发给用户的内容。它使用walk,并编写一个python文件,该文件首先创建所有目录,然后写入文件。我遇到的问题是将文件转换成一个字符串,我可以将其写入文件

这是我的计划:

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)
如果我在一个有on子目录的目录中运行它,它里面的on文件会创建这个文件

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
.'))

没有逃逸,并且在末尾有两个换行符。我认为连载的全部意义在于,你总是可以用同样的方式读回它。有人知道我怎样才能使文件成为一个有效的字符串吗?

对于这个生疏的问题,我很抱歉,我刚刚发现我一直在试图重新发明轮子。显然,这个名称已经存在。

为什么不使用tarfile?@m.wasowski我最终使用了pyzipfile。