时间和系统操作

1引入prettyprinter包

2引入时间包 import time

获取从1970年到现在的秒数

import  time
time_line = time.time()
print(time_line)

time.struct_time

获取本地时间

time1 = time.localtime()
# 得到的是一个结构体
# 0 周日  1-6周一到周六  js
# 0 周一    6周日
print(time1)
print('-------------')
# 获取从1970年开始往后指定的秒数所对应的时间
time2 = time.localtime(1530400000)
print(time2)

设置自定义事件2018-07-02 12:12:12

y year     m month    d day    H hour    M minutes    S second

time4 = time.strftime('%y-%m-%d %H:%M:%S',time.localtime())
print(time4)

sleep线程休眠

while True :
    # 获取当前时间
    time5 = time.localtime()
    print('检测')
    if time5.tm_year == 2018 and time5.tm_mon == 8 and time5.tm_mday == 2 and time5.tm_hour == 10 :
        print('发送邮件')
        break
        # 线程休眠
    time.sleep(1)
    break

3引入日期函数

import datetime

获取现在时间  2018-07-02 11:05:23

date1 = datetime.datetime.today()
print(date1)

获取现在时间

date2 = datetime.datetime.now()
print(date2)

strftime 不能进行中文编码  但是可以把得到的结果进行中文转换

date2 = datetime.datetime.now()
date3 = date2.strftime('%yyear%mmonth%dday')
print(date3.replace('year','年').replace('month','月').replace('day','日'))

设置时间间隔    从现在往后面推迟指定的时间

date4 = datetime.timedelta(hours=12,minutes=30)
print(date4)
date5 = datetime.datetime.now + date4
print(date5)

只获取当前时间

date5=  datetime.datetime.today()
# 只获取当前的日期
date6 = date5.date()
print(date6)
print('{}年{}月{}日'.format(date6.year,date6.month,date6.day)

.date获取日期

.time获取时间

.timestamp()获取时间戳时间线()

date5 =  datetime.datetime.today()
date6 = date5.date()
print(date6)


date7 = date5.time()
print(date7)

print('{}时{}分{}秒'.format(date7.hour,date7.minute,date7.second))

print('当前的时间戳为{}'.format(date5.timestamp()))

4 日历引入日历包    import calender

calen是一个对象

calen = calendar.Calendar()
print(calen)

iterable可以迭代的  for循环

ca1 = calen.iterweekdays()

ca1 = calen.itermonthdays(year=2018,month=7)
ca1 = calen.itermonthdates(year=2018,month=7)
for x in ca1 :
    print(x)

获取文本日历

calen = calendar.TextCalendar()
calen.prmonth(theyear=2018,themonth=7)
import  os
cpu_count = os.cpu_count()
print(cpu_count)

name = os.name
print(name)

result = os.path.exists('1.homewook.py')
if result :
    print('存在')
else :
    print('不存在')
print(result)


#给文本日历指定月份
print(calen)calen.pryear(theyear=2018)print(calen)

5系统操作

operation system  模块获取电脑的相关信息 并且具有很强大的文件以及文件夹操作能力  所以在操作文件和文件夹的时候首先要引入os模块

nt代表windows操作系统

import  os
cpu_count = os.cpu_count()
print(cpu_count)

name = os.name
print(name)

result = os.path.exists('1.homewook.py')
if result :
    print('存在')
else :
    print('不存在')
print(result)

路径相关的操作

result = os.path.exists('C:/Users/a/Desktop/os测试/python.txt')
result = os.path.exists('C:/Users/a/Desktop/os测试')
# 判断文件和目录的路径是否存在
 print(result)
# 当前文件的绝对路径

result = os.getcwd()
#切换路径
os.chdir('test')
print(result)

# absolute 绝对的
# 在计算机当中,获取当前文件路径 用.
# 获取父文件夹路径 用 ..
result = os.path.abspath('.')
print(result)
result = os.path.abspath('..')
print(result)
# 获取指定文件对应的绝对路径
result = os.path.abspath('周二.txt')
print(result)

# 获取文件路径的某一部分  C:/Users/a/Desktop/os测试

result = os.path.basename('C:/Users/a/Desktop/os测试')
print('路径的basename:{}'.format(result))

#当前路径
result = os.path.abspath('.')
print(result)
# 当前路径的父路径
result = os.path.abspath('..')
print(result)
# 找到基名
result = os.path.basename('c:/user/Administrator/Desktop/os.测试')
print(result)
# 注意/将路径分为几部分找到公共的部分  
result = os.path.commonpath(['http://www.baidu.com','http://www.jd.com','http://taobao.com'])
print(result)
#获取父目录
result = os.path.dirname('c:/user/Administrator/Desktop/os.测试/python.txt')
print(result)

获取文件夹信息  创建日期  修改日期  访问日期  大小


import time
# getctime get获取
# c 文档是:change   实际是:create
result = os.path.getctime('C:/Users/a/Desktop/os测试')
print('文件创建日期为:{}'.format(time.localtime(result)))

# a : access 访问
result = os.path.getatime('C:/Users/a/Desktop/os测试/')
print('文件的访问日期是:{}'.format(time.localtime(result)))

# m :modify 修改
result = os.path.getmtime('C:/Users/a/Desktop/os测
result = os.path.isfile('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
print('文件的修改日期是:{}'.format(time.localtime(result)))# size 尺寸;大小 # 获取的大小 为字节大小 Bresult = os.path.getsize('C:/Users/a/Desktop/os测试')

判断是否是文件

result = os.path.isfile('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))

文件分割

# 文件分割-----------------------------------------
# split 分割;
# 分割路径
# 两部分
# 1 .除最后路径外的全部路径
# 2.最后路径
result = os.path.split('C:/Users/a/Desktop/os测试/python.txt')
print('{}'.format(result))
# 1.全部路径
# 2.文件后缀
result =os.path.splitext('C:/Users/a/Desktop/os测试/python.txt')
print('{}'.format(result))

文件夹的增删改操作

# 文件夹增删改操作
# 值1:修改前的名字
# 值2:修改后的名字
if os._exists('happy.txt'):
    os.rename('happy.txt','葫芦娃.mp3')
if os._exists('葫芦娃.mp3'):
#删除文件
 os.remove('葫芦娃.mp3')
# make directory
# os.mkdir('test')
# os.removedir('test')

文件的读写操作

import random
#open函数如果有这个文件的话打开  没有的话创建
f = open('code.txt','w',encoding='utf-8')
for x in range(10000):
    num = random.randint(0,999999)
    num = '%.6d'% num
    f.write(num + '\n')
# 读写完要对文件进行关闭
f.close()

# 大话数据结构  时间复杂度:计算程序运行所花费的时间
f = open('code1.txt','w',encoding='utf-8')
for x in range(10000):
    content = ''
    for y in range(6):
        # 获取一个从0到9的数字
        num = random.randint(0, 9)
        # 将数字转化成字符 并和之前的字符串进行拼接
        content += str(num)
    f.write(content + '\n')
f.close()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值