python 学习笔记【二】【原创】

一、读取配置
#config.ini
[server]
interfaceURI = http://localhost:8080/dub/
downloadURI = http://127.0.0.1:7056/data/
saveDir = D:/temp/singlewindow/data/
agentCode = 3122263935

#config.py
import configparser

def getConfigValue(section, key):
    config = configparser.ConfigParser()
    config.read("config.ini")
    return config.get(section, key)

print(getConfigValue("server","interfaceURI "))


二、读取文件
def readfile(filename):
    f = open(filename,'r',encoding= 'utf8') # 读模式
    s=f.read() # 一次读取整个文件,文件大不适用
    f.close()
    return s


三、多线程
from time import sleep

def downloadmessage():
    print("下载报文线程开始启动...")
    while (True):
        #do download

def uploadreceipt():
    print("上传回执线程开始启动...")
    while (True):
        #do upload


if __name__ == '__main__':
    threads = []
    t1 = threading.Thread(target=downloadmessage)
    threads.append(t1)
    t2 = threading.Thread(target=uploadreceipt)
    threads.append(t2)

    for t in threads:
        t.setDaemon(True)
        t.start()

    t.join()

    print("主线程运行结束...")


四、异常处理
try:
     files = httprequest.getFileByRequests()
     arr=json.loads(files)
     for i in range(0, len(arr)):
         download.downloadFile(arr[i].get('fileName'))
except:
     print("Unexpected error:", sys.exc_info()[0])
finally:
     sleep(1)



五、打包
安装pyinstaller, 安装方法见 python 学习笔记【一】
#打成launcher.exe
pyinstaller -F -i c:\zjport.ico launcher.py

猜你喜欢

转载自zhenggm.iteye.com/blog/2407946