码迷,mamicode.com
首页 > 编程语言 > 详细

python中打印输出date信息

时间:2017-05-25 22:11:00      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:python

python中要打印显示linux命令行date命令的相关信息,有多种方法:


方法1:直接调用linux命令输出;同样也可以打印主机名;

[root@host74 tmp]# cat 1.py

#!/usr/bin/python

import os,commands

hostname = commands.getoutput(‘hostname‘)

date = commands.getoutput(‘date‘)

print hostname

print date


[root@host74 tmp]# python 1.py

host74

2017年 05月 25日 星期四 16:05:16 CST


方法2:和方法1执行结果一样;

[root@host74 tmp]# cat 2.py

#!/usr/bin/python

import os

os.system("date")


[root@host74 tmp]# python 2.py

2017年 05月 25日 星期四 16:06:39 CST



方法3:使用time模块ctime() 显示格式看起来不太直观;

[root@host74 tmp]# cat 2.py

#!/usr/bin/python

import time

print time.ctime()


[root@host74 tmp]# python 2.py

Thu May 25 16:05:50 2017


方法4:使用time模块,按年月日时间显示,比较直观;

[root@host74 tmp]# cat 2.py

#!/usr/bin/python

import time

print time.strftime(‘%Y-%m-%d %H:%M:%S‘,time.localtime(time.time()))


[root@host74 tmp]# python 2.py

2017-05-25 16:06:03




本文出自 “模范生的学习博客” 博客,请务必保留此出处http://mofansheng.blog.51cto.com/8792265/1929464

python中打印输出date信息

标签:python

原文地址:http://mofansheng.blog.51cto.com/8792265/1929464

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!