python - py2exe:代码一旦转换就不会修改脚本

标签 python dictionary py2exe

作为一个小项目,我创建了一个小型聊天室。代码如下,我可以指出我是编程新手,因此结构可能效率不高:

import re, time
user_name = 0
password = 0
chatroom = 0

from users import users
def startup():
      global user_name, password, chatroom
      print "\n" * 100
      user_name = raw_input("Your username: ").upper()
      if users.has_key(str(user_name).lower()) == True:
            password = str(raw_input("Password: "))
            while str(users[user_name.lower()]) != str(password):
                  print "Incorrect password."
                  time.sleep(2)
                  startup()
            else:
                  chatroom = raw_input("Room name: ").lower()
                  chat()
      else:
            print "Invalid username."
            time.sleep(2)
            startup()

def showchat():
      global user_name
      file = open(str(chatroom) + ".txt","r+")
      messages = str(file.read()[-700:])
      file.close
      messages = messages.rstrip('\n')
      print "\n" * 40
      print messages
      print "------ type 'r' to refresh the screen ------"

def writechat():
      global user_name, chatroom
      n = raw_input("________________________________________________________" + "\n" + user_name + ": ")      
      if user_name.lower() == 'admin':
            if n == "clear":
                  file = open(str(chatroom) + ".txt","w")
                  file.write("")
                  file.close
            elif n == "addnewuser":
                  x = "'" + raw_input("new username: ") + "'"
                  y = "'" + raw_input("new password: ") + "'"
                  file = open("users.py","r").read()
                  file = file.replace("'username' : 'password',", x + " : " + y + "," + "'username' : 'password',")
                  open("users.py","w").write(file)
            elif n == "r":
                  chat()
            elif n == "logout":
                  startup()
            else:
                  file = open(str(chatroom) + ".txt","a")
                  file.write(user_name + ": " + str(n) + "\n")
                  file.close()
      else:
            if n == "r":
                  chat()
            elif n == "changepassword":
                  file = open("users.py","r")
                  file.read()
                  oldpass = raw_input("New password: ")
                  users[user_name.lower()] = oldpass
                  print users
                  file.close()
                  file = open("users.py","w")
                  file.write("users = " + str(users))         #here
                  file.close()
            elif n == "logout":
                  startup()
            else:
                  file = open(str(chatroom) + ".txt","a")
                  file.write(user_name + ": " + str(n) + "\n")
                  file.close()

def chat():
      showchat()
      writechat()
      chat()


startup()

只要 chatroom.txt 文件和 users.py 都存在,这段代码就可以在 python 中完美运行。 一旦我将其转换为 .exe,就会出现问题,它工作正常,只是 users.py 文件在应该时没有被永久写入(它是暂时的),相关编码被标记为 #here。

我的设置代码如下:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe' : {
'packages': ['users'],
"bundle_files" : 2,
}},
    console = [{'script': "chatroom.py"}],
    zipfile = None,
)

users.py 只是一个 (user:password) 的字典:

users = {
'admin' : '2588619',
'john' : '1234',
'username' : 'password',
}

那么如何将其转换为 .exe 并使其永久写入 users.py?

最佳答案

py2exe 创建的包只是一个自动解压存档,其中包含 python 解释器和您的程序。每次运行 .exe 时,里面的所有内容都会被提取到临时目录中,并且程序由提取的解释器运行。 这意味着如果您将 users.py 与源代码放在一起,您想要实现的目标根本不可能实现。

您应该在某个用户目录中创建一个users.py文件,例如:

users = open(os.path.join(os.path.expanduser('~'), '_MyProgram', 'users.py'))

显然,您还应该确保该目录存在,并最终创建它。

这可以通过 py2exe 传递 data_files 自动完成。参数。

关于python - py2exe:代码一旦转换就不会修改脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926995/

相关文章:

python - 摄影(或匹配两张照片)

Python-将许多列表字典添加到持久主字典时的性能

python - py2exe - fatal error : Could not locate script

python - 如何检查列表中的所有项目

python - Python/Django 中模型/对象实例的字符串字典表示?

python - 使用 py2exe 或 cx_freeze 生成可执行文件时出现 APScheduler 导入错误

python - 使用py2exe时出现名称错误

python - brew install python3,但无法链接到python3

python - 逻辑回归 PMML 不会产生概率

python - Twisted:如何在初始连接时识别协议(protocol),然后委托(delegate)给适当的协议(protocol)实现?