exe created using cx_freeze not working properly

白昼怎懂夜的黑 提交于 2019-12-25 03:26:11

问题


 use=input('what do you wanna do \n1.press w to create a new file\n2.press r to read a       file:\n')

 if use=='r':
    read()
 elif use=='w':
    write()
 else :
    print('OOPS! you enter a wrong input\n')
    user() 

when i run this code using IDLE it runs properly but when i created a exe of this python file using cx_freeze then the if and elif conditions are not working for 'r' and 'w' respectively. for any input it always goes to the else statement.

I am using python 3.2 and cx_freeze 3.2


回答1:


Just for a quick test, I did this:

use = input("test input here: ")

for i in use:
    print(ord(i))

The result, if you type in "hello", is the ascii character codes for hello, plus "13". This is \r, the return character, which is being added to your string. This doesn't happen under Linux and is the result of the fact on Windows a newline is \r\n as opposed to just \n.

The workaround for you would be to do something like:

use = input("test input: ").strip("\r")

strip() is a string object method that'll remove characters from the end and beginnings of strings.

Notes:

  1. The use of ord() in the above example is probably not best practise - see Unicode.
  2. If you ever write GUIs and use cx_freeze, don't use print() or input() - on Windows the standard input/output handles don't exist for GUI apps at all. That tripped me up for a while with cx_freeze + gui code. Just a note for when you get there.


来源:https://stackoverflow.com/questions/8935043/exe-created-using-cx-freeze-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!