标签:
模块,用一砣代码实现了某个功能的代码集合。
类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
如:os 是系统相关的模块;file是文件操作相关的模块
模块分为三种:
2、导入模块
Python之所以应用越来越广泛,在一定程度上也依赖于其为程序员提供了大量的模块以供使用,如果想要使用模块,则需要导入。导入模块有一下几种方法:
1 import module 2 from module.xx.xx import xx 3 from module.xx.xx import xx as rename 4 from module.xx.xx import *
导入模块其实就是告诉Python解释器去解释那个py文件
1、导入一个py文件,解释器解释该py文件
2、导入一个包,解释器解释该包下的 __init__.py 文件
那么问题来了,导入模块时是根据那个路径作为基准来进行的呢?即:sys.path
如果sys.path路径列表没有你想要的路径,可以通过 sys.path.append(‘路径‘) 添加。
通过os模块可以获取各种目录,例如:
>>> import sys >>> print (sys.path) [‘‘, ‘/usr/local/lib/python3.5/site-packages/Django-1.9.7-py3.5.egg‘, ‘/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip‘, ‘/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5‘, ‘/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin‘, ‘/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload‘, ‘/usr/local/lib/python3.5/site-packages‘] >>>
如果sys.path路径列表没有你想要的路径,可以通过 sys.path.append(‘路径‘) 添加。
通过os模块可以获取各种目录,例如:
import os,sys if platform.system() == "Windows": BASE_DIR = ‘\\‘.join(os.path.abspath(os.path.dirname(__file__)).split(‘\\‘)[:-1]) print(BASE_DIR) else: BASE_DIR = ‘/‘.join(os.path.abspath(os.path.dirname(__file__)).split(‘/‘)[:-1]) sys.path.append(BASE_DIR)
time &datetime模块
#_*_coding:utf-8_*_
import timeimport datetimeprint(time.clock()) #返回处理器时间,3.3开始已废弃print(time.process_time()) #返回处理器时间,3.3开始已废弃print(time.time()) #返回当前系统时间戳print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间>>> print(time.ctime())
Sat Jun 18 07:22:48 2016
>>>
print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式>>> print(time.ctime(time.time()-86640))
Fri Jun 17 07:19:37 2016
>>>
print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式>>> print(time.gmtime(time.time()-86640))
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=16, tm_hour=23, tm_min=20, tm_sec=17, tm_wday=3, tm_yday=168, tm_isdst=0)
>>>
print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间>>> print(time.localtime(time.time()-86640))
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=17, tm_hour=7, tm_min=21, tm_sec=18, tm_wday=4, tm_yday=169, tm_isdst=0)
>>>
print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式>>> print(time.mktime(time.localtime()))
1466205958.0
>>>
#time.sleep(4) #sleepprint(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式>>> print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
2016-06-17 23:26:40
>>>
print(time.strptime("2016-6-16","%Y-%m-%d")) #将字符串格式转换成struct_time格式>>> print(time.strptime("2016-6-16","%Y-%m-%d"))
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=168, tm_isdst=-1)
>>>
#datetime moduleprint(datetime.date.today()) #输出格式 2016-06-18>>> print(datetime.date.today())
2016-06-18
>>>
print(datetime.date.fromtimestamp(time.time()-864400)) # 将时间戳转成日期格式>>> print(datetime.date.fromtimestamp(time.time()))
2016-06-18
>>>
current_time = datetime.datetime.now() #>>> print(datetime.datetime.now())
2016-06-18 07:32:52.193779
print(current_time) #输出2016-01-26 19:04:30.335935print(current_time.timetuple()) #返回struct_time格式>>> print(datetime.datetime.now().timetuple())
time.struct_time(tm_year=2016, tm_mon=6, tm_mday=18, tm_hour=7, tm_min=35, tm_sec=40, tm_wday=5, tm_yday=170, tm_isdst=-1)
>>>
#datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120sprint(new_date)
标签:
原文地址:http://www.cnblogs.com/zcx-python/p/5572108.html