2020.05.20python学习

time 时间模块

import time

time() 获取本地时间戳

res = time.time()
print(res)

localtime -> mktime ->ctime
locatime() 获取本地时间元组 (参数是时间戳,默认当前)

"""
time.struct_time(tm_year=2020, tm_mon=5, tm_mday=20, tm_hour=9, tm_min=36, tm_sec=43, tm_wday=2, tm_yday=141, tm_isdst=0)
"""
ttp = time.localtime()# 默认返回当前时间戳对应的时间元组
print(ttp)
ttp = time.locatime(1241415)#默认返回当前时间戳对应的时间元组
print(ttp)

mktime() 通过时间元组获取时间戳(参数是时间元组)
年 月 日 时 分 秒 0 0 0

ttp = (2020,5,20,9,40,30,0,0,0)
res = time.mktime(ttp)
print(res)

ctime() 获取本地时间字符串 (参数是时间戳,默认当前)

res = 12414135245
strvar= time.ctime() #默认返回当前时间戳对应的时间字符串
strvar = time.ctime(res)
print(strvar)

asctime() 通过时间元组获取时间字符串(参数是时间元组) 了解

'''不能自动识别今天是周几'''
ttp = (2020,5,20,9,40,30,2,0,0)
res = time.asctime(ttp)
print(res)
'''
strftime 元组->字符串
strpttime 字符串 -> 元组
'''

sleep() 程序睡眠等待

time.sleep(1)
print('wake up')

strftime() 格式化时间字符串(格式化字符串,时间元组)

'''linux系统下 支持中文,windows系统不支持中文'''
strvar = time.strftime('%H:%M:%S %Y-%m-%d') #默认当前时间
print(strvar)

ttp = (2008,10,1,11,11,11,0,0,0)
res = time.strftime("%Y-%m-%d %H:%M:%S" , ttp) # 指定时间元组
print(res)

strptime() 将时间字符串通过指定格式提取到时间元组中(时间字符串,格式化字符串)

strvar = "2020年5月20号11时12分13秒,今天上街要是谁的玫瑰花那个刺扎到我了,没个3,5万,我绝对不起来"
ttp = time.strptime(strvar,"%Y年%m月%d号%H时%M分%S秒,今天上街要是谁的玫瑰花那个刺扎到我了,没个3,5万,我绝对不起来")
print(ttp)

perf_counter() 用于计算程序运行的时间 (了解)

# starttime = time.perf_counter()
starttime = time.time()
for i in range(100000000):
	pass
endtime =  time.time()
# endtime = time.perf_counter()
print(endtime - starttime)

os模块 – 对系统进行操作

import os

system() 在python中执行系统命令

os.system("calc")
os.system("mspaint")
os.system("type nul>ceshi.go") 创建文件
os.system("ipconfig") 乱码

popen() 执行系统命令返回对象,通过read方法读出字符串

obj = os.popen("ipconfig")
print(obj)

windows 默认的gbk编码, 通过read方法可以直接转换为utf-8编码集

print(obj.read())

listdir() 获取指定文件夹中所有内容的名称列表
路径: 相对路径 , 绝对路径(完整路径))
. 代表当前目录,
代表上一级目录

相对于当前

res = os.listdir(".")

相对于上一级

res = os.listdir("..")

绝对路径

res = os.listdir(r"E:\python30\day15\代码")
print(res) # ['1.py', '2.py', 'part11.md']

getcwd() 获取当前文件所在的默认路径

res = os.getcwd()
print(res) # E:\python30\day16

chdir() 修改当前文件工作的默认路径
os.chdir(r"E:\python30")

os.chdir("..")
os.system("type nul>abcd123.php")

environ 获取或修改环境变量
print(os.environ)

PATH :敲命令,让系统通过路径找到对应系统文件,进行执行

print(os.environ["PATH"])

将路径添加到环境变量中,在执行命令时,通过路径去找对应的对应,进行执行.

# os.environ["PATH"] += r"D:\qq\Bin;"
# os.system("QQScLauncher")

–os 模块属性
name 获取系统标识 linux,mac ->posix windows -> nt

print(os.name)

*sep 获取路径分割符号 linux,mac -> / window-> *

print(os.sep)

linesep 获取系统的换行符号 linux,mac -> \n window->\r\n 或 \n 了解

res = repr(os.linesep)
print(res)

os 与 shutil

import os
os.chdir(r"E:\python30\ceshi111")

os 创建和删除 文件和文件夹
shutil 复制和剪切 文件和文件夹

– os模块具有 新建/删除/
os.mknod 创建文件 (linux可以,windows有兼容问题)

 os.mknod("abc.py")
 os.system("type nul>abc.py")

os.remove 删除文件

 os.remove("abc.py")

os.mkdir 创建目录(文件夹)

 os.mkdir("ceshi100")

os.rmdir 删除目录(文件夹)

 os.rmdir("ceshi100")

os.rename 对文件,目录重命名

os.rename("ceshi100","ceshi200")

os.makedirs 递归创建文件夹

os.makedirs("a/b/c/d")

os.removedirs 递归删除文件夹(空文件夹)

 os.removedirs("a/b/c/d")

– shutil模块 复制和剪切

import shutil

copy(src,dst) #复制文件权限和内容

shutil.copy("ceshi300.txt","ceshi4.txt")

copytree(src,dst) #拷贝文件夹里所有内容(递归拷贝)

 shutil.copytree("a","ccc")

rmtree(path) #删除当前文件夹及其中所有内容(递归删除)

 shutil.rmtree("a")

move(path1,paht2) #移动文件或者文件夹

shutil.move("ceshi300.txt","../ceshi999.php")

os.path 路径模块

import os
pathvar = r"E:\python30\day16\abc.py"

basename() 返回文件名部分

res = os.path.basename(pathvar)
print(res)

dirname() 返回路径部分

res = os.path.dirname(pathvar)
print(res)

split() 将路径拆分成单独的文件部分和路径部分 组合成一个元组(了解)

print(os.path.split(pathvar))

*join() 将多个路径和文件组成新的路径 可以自动通过不同的系统加不同的斜杠 linux / windows*

path1 = "E:"
path2 = "python30"
path3 = "day16"

正常

strvar = path1 +  path2 + os.sep + path3
print(strvar)

strvar = os.path.join(path1,path2,path3)
print(strvar)

splitext() 将路径分割为后缀和其他部分 (了解)

 print(os.path.splitext(pathvar))
 print(pathvar.split(".")[-1])

getsize() 获取文件的大小 (只能是文件)

res = os.path.getsize(pathvar)
print(res)

is系列
isdir() 检测路径是否是一个文件夹

res = os.path.isdir(pathvar) # False

isfile() 检测路径是否是一个文件

res = os.path.isfile(pathvar) # True

islink() 检测路径数否是一个链接 (了解)

res = os.path.islink(pathvar) # False
print(res)

获取时间系列
getctime() [windows]文件的创建时间,[linux]权限的改动时间(返回时间戳)

res = os.path.getctime(pathvar)

getmtime() 获取文件最后一次修改时间(返回时间戳)

res = os.path.getmtime(pathvar)

getatime() 获取文件最后一次访问时间(返回时间戳)

res = os.path.getatime(pathvar)
import time
strvar = time.ctime(res)
print(strvar)

exists() 检测指定的路径是否存在

pathvar = r"E:\python30\day16\abcdefg.py"
res = os.path.exists(pathvar)
print(res)

isabs() 检测一个路径是否是绝对路径
abspath() 将相对路径转化为绝对路径

pathvar = "."
# res = os.path.isabs(".")
# print(res)
if not os.path.isabs("."):
	res = os.path.abspath(pathvar)
	print(res)

你可能感兴趣的:(python学习)