六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

python 之 生成临时文件

[复制链接]

升级  68.2%

265

主题

265

主题

265

主题

进士

Rank: 4

积分
841
 楼主| 发表于 2013-2-7 09:06:30 | 显示全部楼层 |阅读模式
#!/usr/bin/evn pythonimport os;import sys;import uuid;def WriteStrToFile(strFile, strVal):    bRet = False;    file = None;    try:        file = open(strFile, "wb");        file.write(strVal);        bRet = True;    finally:        if file:            file.close();    return bRet;if "__main__" == __name__:    strVal = "hell, andylin!";        #file1:    strFile1 = ("%s" %uuid.uuid1());    print("filename1 = %s" %strFile1);    WriteStrToFile(strFile1, strVal);        #file2:    strFile2 = ("%s" %uuid.uuid3(uuid.NAMESPACE_DNS, "hello,andylin!"));    print("filename2 = %s" %strFile2);    WriteStrToFile(strFile2, strVal);        #file3:    strFile3 = ("%s" %uuid.uuid4());    print("filename3 = %s" %strFile3);    WriteStrToFile(strFile3, strVal);        #file4:    strFile4 = ("%s" %uuid.uuid5(uuid.NAMESPACE_DNS, "hello,andylin!"));    print("filename4 = %s" %strFile4);    WriteStrToFile(strFile4, strVal);     
结果:
filename1 = 0cc887b6-074a-11e0-b344-0023ae99236c
filename2 = eb7e4058-67ef-389b-bf9f-ad74cecb03a2
filename3 = c9cd1854-e509-4915-a841-49ff3ff851cb
filename4 = e1e49243-43a9-5700-b440-35fda02c4d96
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表