python--系统时间的操作

一、查看文件的生成时间并写入新的文件

time1 = os.path.getctime('/etc/shadow')
print(time1)
tuple_time = time.localtime(time1)   ##用time.localtime()方法,将一个时间戳转换为当前时区的struct_time
print(tuple_time)
year = tuple_time.tm_year
month = tuple_time.tm_mon
day = tuple_time.tm_mday

with open('file1.txt', 'a') as f:
    f.write('%d-%d-%d' % (year, month, day))
    f.write('\n')

二、时间格式之间转换

1,元组时间转换为时间戳

uple_time = time.localtime()
print(tuple_time)
print(time.mktime(tuple_time))

输出结果:time.struct_time(tm_year=2021, tm_mon=1, tm_mday=19, tm_hour=23, tm_min=18, tm_sec=24, tm_wday=1, tm_yday=19, tm_isdst=0)
1611069504.0

2, 时间戳转换为字符串时间

pwd_time = os.path.getctime('/etc/shadow')
print('pwd_time', pwd_time)
print(time.ctime(pwd_time))

输出结果:pwd_time 1597407832.5074072    Fri Aug 14 20:23:52 2020

3、时间戳转换为元组时间

print(time.localtime(pwd_time))

综合练习(os和time)

需求:
1,获取当前主机的信息,包含操作系统名,主机名,内核版本,硬件架构
2,获取开机时间和开机时长
3,获取当前登录用户

注意:psutil 模块需要重新下载安装

import os                                                                          
import psutil                                                                      
from datetime import datetime                                                      
print('主机的信息'.center(50,'*'))                                                      
info = os.uname()                                                                  
print(                                                                             
    """                                                                            
     操作系统: %s                                                                      
     主机名称: %s                                                                      
     内核版本: %s                                                                      
     硬件架构: %s                                                                      
    """                                                                            
     %(info.sysname, info.nodename, info.release, info.machine))                   
                                                                                   
print('开机信息'.center(50, '*'))                                                      
boot_time = psutil.boot_time()                                                     
boot_time_obj = datetime.fromtimestamp(boot_time)                                  
#print(type(boot_time_obj))                                                        
now_time = datetime.now()                                                          
time1 = now_time - boot_time_obj                                                   
print('开机时间',boot_time_obj)                                                        
print('开机时间', str(now_time).split('.')[0])                                         
print('开机时长', str(time1).split('.')[0])                                            
                                                                                   
print('当前登录用户'.center(50, '*'))                                                    
login_user = psutil.users()                                                        
print(login_user[0].name)   


输出结果:
**********************主机的信息***********************

     操作系统: Linux 
     主机名称: server1
     内核版本: 3.10.0-957.el7.x86_64
     硬件架构: x86_64
    
***********************开机信息***********************
开机时间 2021-01-19 18:56:59
开机时间 2021-01-20 00:52:44
开机时长 5:55:45
**********************当前登录用户**********************
root
                                   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值