python3.6sysos_python入门----3+1个内置模块:sys,os, time,datetime

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))      #  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) # ['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")))#

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())

# Sun Feb 18 21:23:42 2018

print(type(time.localtime()),time.localtime())

#

#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())

# 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

标签:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值