您的位置:首页 > 编程语言 > Python开发

简单Python实例,以及打包成.exe文件

2014-07-04 17:16 706 查看
1、工具准备:

我的Python版本是python 3.x的

打包工具:cxfreeze,我的是 windows 32bit的 http://sourceforge.net/projects/cx-freeze/files/4.3.2/

关联你的eclipse:

help->install new solftware->输入插件地址:http://download.aptana.com/studio3/plugin/install, 访问download.aptana.com/studio3会提示叫你把前面的地址输入到下面的work with 框中。

选中,然后一路next 下去。

安装成功之后, window->preferences->pyDev图片下面的最后一项,关联安装的python.exe



然后创建python的project呗:

写了一个简单的例子:

'''

Created on 2014-7-4

@author: liyao

'''

import os

oldWorld = None

newWorld = None

fileType = None

# scan the path you entered, then replace aim files

def scanDir(path, oldWorld, newWorld, fileType):

  for root, dirs, files in os.walk(path):

    for filespath in files:

        if os.path.join(root, filespath).endswith("."+fileType):

            replaceFun(os.path.join(root, filespath), oldWorld, newWorld)

# replace the old world as the new world         

def replaceFun(filepath, oldWorld, newWorld):

    lines = open(filepath, "r", encoding= "utf-8").readlines()

    fp = open(filepath, "w")

    for s in lines: 

        fp.write(s.replace(oldWorld, newWorld))

    fp.close

flag = None

while 1:

    print("==================================================")

    print("======            0、退出当前应用            =======")

    print("======            1、进入当前应用            =======")

    print("======                                     =======")

    print("==================================================")

    flag = input("编号:")

    if flag == "0":

        print("谢谢使用")

        break

    elif flag == "1":

        dir = input("enter your project,like:E:\python\Hellowin     path=")                                                                                                                                                                                      
                                                                                                                                                                                                                                                               
                                                                    

        if not dir.strip():

            print("is null dir")

            exit(-1)

         

        oldWorld = input("enter your old string  =")

        if not oldWorld.strip():

            print("is null world")

            exit(-1)

         

        newWorld = input("enter your new string  =")

        if not newWorld.strip():

            print("is null world")

            exit(-1)

            

        fileType = input("enter your file type,like:java,txt,xml type=             ")

        if not fileType.strip():

            print("is null world")

            exit(-1)

        scanDir(dir, oldWorld, newWorld, fileType)

    

打包:

1、安装cxfreeze

2、C:\Python33\Scripts的目录下面会有的,安装它,会自动匹配到你的python安装文件路径下面。



把命令转到对应Scripts下面,才能执行cxfreeze的命令:

找到你的.py文件,通过命令:

如:cxfreeze C:\Python33\Scripts\Project.py --target-dir E:\然后会生成.exe等相关文件,这样打包就成功了:



参考:
http://huangda-hd.blog.163.com/blog/static/81808426201361741158158/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python java