python入门----3+1个内置模块:sys,os, time,datetime

2022-09-19 10:37:14

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-01-04
print(datetime.datetime.now()+datetime.timedelta(minutes=3))
                        #2018-02-18 21:29:43.768354


  • 作者:根哥的博客
  • 原文链接:https://blog.csdn.net/eyeofeagle/article/details/79335741
    更新时间:2022-09-19 10:37:14