摘要: import pickle# 序列化 用于对Python对象进行序列化和反序列化的二进制协议f = open("pickle.txt", "wb+")lists = [123, "中文", [456]]strs = "字符串"num = 123# 写入pickle.dump(lists, f) # 阅读全文
posted @ 2017-10-26 11:36 魏桐 阅读(223) 评论(0) 推荐(0) 编辑
摘要: import os path = os.getcwd() # 获取当前目录print("路径: {}".format(path)) # 路径: E:\python\练习\笔记dirname = os.path.dirname(path) # 获取文件夹名print("文件夹名为: {}".forma 阅读全文
posted @ 2017-10-26 11:27 魏桐 阅读(101) 评论(0) 推荐(0) 编辑
摘要: import osimport signal # 执行命令dirs = os.popen("dir").read()print(dirs)# 打印目录树dirs_info = os.scandir()for info in dirs_info: print("文件名: {}, 路径: {}, ino 阅读全文
posted @ 2017-10-26 11:16 魏桐 阅读(385) 评论(0) 推荐(0) 编辑
摘要: import calendarimport timecalen_text = calendar.TextCalendar()# 打印月历calen_text.prmonth(2017, 5, w=0, l=0)# 打印年历calen_text.pryear(2017, w=2, l=1, c=6, 阅读全文
posted @ 2017-10-26 10:37 魏桐 阅读(286) 评论(0) 推荐(0) 编辑
摘要: import datetime import time datetime_dt = datetime.datetime.today() # 获取当前日期和时间, 2017-10-26 10:03:28.693198datetime_str = datetime_dt.strftime("%Y/%m/ 阅读全文
posted @ 2017-10-26 10:33 魏桐 阅读(665) 评论(0) 推荐(0) 编辑
摘要: import timedef time_demo(): curtime = time.time() # 获取当前时间戳, 1508982743.220433 time_str = time.ctime(curtime) # 转为string格式, 'Thu Oct 26 09:52:23 2017' 阅读全文
posted @ 2017-10-26 10:03 魏桐 阅读(490) 评论(0) 推荐(0) 编辑
摘要: import random # 随机数模块 lists = [1, 2, 3, 4, 5] def demo(): # 产生[0, 100]随机整数 num = random.randint(0, 100) print(num) # 产生[0, 100)随机浮点数 fnum = random.uni 阅读全文
posted @ 2017-10-25 17:28 魏桐 阅读(1410) 评论(0) 推荐(0) 编辑
摘要: import sys# 捕获异常 (可灵活组合) def excep(): # - try except - try: print(ex) except: # 捕获所有异常 print("捕获异常!") try: print(ex) except: # 通过函数获取异常信息 types, value 阅读全文
posted @ 2017-10-25 17:26 魏桐 阅读(170) 评论(0) 推荐(0) 编辑
摘要: # 调用自定义模块 #coding=utf-8# mymodule.py 自定义模块def myfunction(): return "myFunction"# 避免外界调用函数时运行了测试代码if __name__ == "__main__": print(myfunction()) # 导入模块 阅读全文
posted @ 2017-10-25 17:25 魏桐 阅读(304) 评论(0) 推荐(0) 编辑
摘要: # sorted(iterable,key=None,reverse=False)# key接受一个函数,这个函数只接受一个元素,默认为None# reverse是一个布尔值。默认为False排在前,True排在后,升序,即符合条件的往后排# 按照年龄来排序students = [('john', 阅读全文
posted @ 2017-10-25 17:23 魏桐 阅读(141) 评论(0) 推荐(0) 编辑