python入门----3+1个内置模块:sys,os, time,datetime
2018-02-18 22:10
931 查看
1, sys模块:常用函数import sys,time
a=sys.version
print(a) #3.6.0 (default, Feb 12 2018, 19:43:47)
#[GCC 4.8.4]
print(type(a),len(a)) #<class 'str'> 51
#打印“#”: 共10个,间隔一秒出现一个
for i in range(10):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(1)
print("请输入10个字符:")
ms=sys.stdin.read(10)
print(ms)
#退出python
sys.exit("yes---") #yes---2,os模块: 常用函数import os
print(os.getcwd())#/home/daitoue/eclipse_workspace/python1/内置模块
print(os.chdir("/"))
print(os.getcwd())#/home/daitoue
li=os.listdir("/home/daitoue")
print(type(li), li) #<class 'list'> ['pppy.java', 'examples.desktop','图片', '音乐',下载]
#创建、删除、重命名 目录,文件
os.mkdir("python_os1")
os.makedirs("/home/daitoue/py_os2/test/a/b")
os.removedirs("py_os2/test/a/b")
os.renames("p_file2.txt", "a.txt")
os.remove("a.py")
#使用系统命令:
os.system("touch pppy.java")
print(os.name)#posix -->linux系统
#拆分: 文件路径=目录+文件名
print(os.path.split("/home/daitoue/aa/bb/a.txt"))#('/home/daitoue/aa/bb', 'a.txt')
print(os.path.dirname("/home/daitoue/aa/bb/a.txt"),
os.path.basename("/home/daitoue/aa/bb/a.txt"))#/home/daitoue/aa/bb a.txt
print(type(os.path.basename("/home/daitoue/aa/bb/a.txt")))#<class 'str'>
print(os.path.getatime("/home/daitoue/aa/bb/a.txt"))#1518943364.5923748
3, time, datetime 时间日期模块:常用函数import time
import datetime
print(time.time()) #1518960222.8357334
print(type(time.ctime()),time.ctime())
# <class 'str'> Sun Feb 18 21:23:42 2018
print(type(time.localtime()),time.localtime())
#<class 'time.struct_time'>
#time.struct_time(tm_year=2018, tm_mon=2, tm_mday=18, tm_hour=21,
# tm_min=23, tm_sec=42, tm_wday=6, tm_yday=49, tm_isdst=0)
gm=time.gmtime()
print(type(gm), gm) #同上
print(gm.tm_year,gm.tm_mon,gm.tm_mday, gm.tm_hour)#2018 2 18 13
print(time.asctime(time.localtime())) #Fri Feb 16 17:05:59 2018
print(time.asctime((2022,2,16,17,4,3,3,5,6))) #Thu Feb 16 17:04:03 2022
print(time.strptime("2016/05/22","%Y/%m/%d"))
#time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0,
print(time.strptime("2016.05.22","%Y.%m.%d"))#同上
print(time.mktime(time.strptime("2016.05.22","%Y.%m.%d")))#1463846400.0
print(time.mktime(time.localtime())) #1518960403.0
print(time.time()) #1518960403.7676084
print(time.gmtime(234235))#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=3,
print(time.strftime("%Y",((2022,2,16,17,4,3,3,5,6))))#2022
#datetime常用函数
print(datetime.date(2022,2,16))#2022-02-16
print(type(datetime.datetime.now()),datetime.datetime.now())
#<class 'datetime.datetime'> 2018-02-18 21:26:43.767970
print("replace(2h,3min)",datetime.datetime.now().replace(minute=3,hour=2))
# replace(2h,3min) 2018-02-18 02:03:43.768161
print(datetime.date.fromtimestamp(234235))#1970-
4000
01-04
print(datetime.datetime.now()+datetime.timedelta(minutes=3))
#2018-02-18 21:29:43.768354
a=sys.version
print(a) #3.6.0 (default, Feb 12 2018, 19:43:47)
#[GCC 4.8.4]
print(type(a),len(a)) #<class 'str'> 51
#打印“#”: 共10个,间隔一秒出现一个
for i in range(10):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(1)
print("请输入10个字符:")
ms=sys.stdin.read(10)
print(ms)
#退出python
sys.exit("yes---") #yes---2,os模块: 常用函数import os
print(os.getcwd())#/home/daitoue/eclipse_workspace/python1/内置模块
print(os.chdir("/"))
print(os.getcwd())#/home/daitoue
li=os.listdir("/home/daitoue")
print(type(li), li) #<class 'list'> ['pppy.java', 'examples.desktop','图片', '音乐',下载]
#创建、删除、重命名 目录,文件
os.mkdir("python_os1")
os.makedirs("/home/daitoue/py_os2/test/a/b")
os.removedirs("py_os2/test/a/b")
os.renames("p_file2.txt", "a.txt")
os.remove("a.py")
#使用系统命令:
os.system("touch pppy.java")
print(os.name)#posix -->linux系统
#拆分: 文件路径=目录+文件名
print(os.path.split("/home/daitoue/aa/bb/a.txt"))#('/home/daitoue/aa/bb', 'a.txt')
print(os.path.dirname("/home/daitoue/aa/bb/a.txt"),
os.path.basename("/home/daitoue/aa/bb/a.txt"))#/home/daitoue/aa/bb a.txt
print(type(os.path.basename("/home/daitoue/aa/bb/a.txt")))#<class 'str'>
print(os.path.getatime("/home/daitoue/aa/bb/a.txt"))#1518943364.5923748
3, time, datetime 时间日期模块:常用函数import time
import datetime
print(time.time()) #1518960222.8357334
print(type(time.ctime()),time.ctime())
# <class 'str'> Sun Feb 18 21:23:42 2018
print(type(time.localtime()),time.localtime())
#<class 'time.struct_time'>
#time.struct_time(tm_year=2018, tm_mon=2, tm_mday=18, tm_hour=21,
# tm_min=23, tm_sec=42, tm_wday=6, tm_yday=49, tm_isdst=0)
gm=time.gmtime()
print(type(gm), gm) #同上
print(gm.tm_year,gm.tm_mon,gm.tm_mday, gm.tm_hour)#2018 2 18 13
print(time.asctime(time.localtime())) #Fri Feb 16 17:05:59 2018
print(time.asctime((2022,2,16,17,4,3,3,5,6))) #Thu Feb 16 17:04:03 2022
print(time.strptime("2016/05/22","%Y/%m/%d"))
#time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0,
print(time.strptime("2016.05.22","%Y.%m.%d"))#同上
print(time.mktime(time.strptime("2016.05.22","%Y.%m.%d")))#1463846400.0
print(time.mktime(time.localtime())) #1518960403.0
print(time.time()) #1518960403.7676084
print(time.gmtime(234235))#time.struct_time(tm_year=1970, tm_mon=1, tm_mday=3,
print(time.strftime("%Y",((2022,2,16,17,4,3,3,5,6))))#2022
#datetime常用函数
print(datetime.date(2022,2,16))#2022-02-16
print(type(datetime.datetime.now()),datetime.datetime.now())
#<class 'datetime.datetime'> 2018-02-18 21:26:43.767970
print("replace(2h,3min)",datetime.datetime.now().replace(minute=3,hour=2))
# replace(2h,3min) 2018-02-18 02:03:43.768161
print(datetime.date.fromtimestamp(234235))#1970-
4000
01-04
print(datetime.datetime.now()+datetime.timedelta(minutes=3))
#2018-02-18 21:29:43.768354
相关文章推荐
- 【Python模块】os & linecache & hashlib & commands & sys & time & datetime
- Python模块:time、datetime、random、os、sys
- Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用
- Python内置模块之time、random、hashlib、OS、sys、UUID模块
- Python常用模块-SYS、OS、Time、hashlib
- python内置模块[sys,os,os.path,stat]
- python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)
- 《每日一课》Python入门之datetime内置模块的使用
- python的基础模块(os,sys,time,shutil,differlib,socket)
- Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
- sys/os/内置模块 Python
- Python内置的操作系统模块(os)与解释器交互模块(sys)
- day6 模块time datetime random os sys json pikle
- python内置模块[sys,os,os.path,stat]
- python sys,os,time模块的使用(包括时间格式的各种转换)
- Python笔记(5):sys模块,os模块,set,heap,deque简介
- python datetime模块和time模块
- Python3用sys和time模块实现进度条
- Python模块 - os , sys.shutil
- Python模块知识2:时间日期日历模块Time、Datetime、Calendar