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
|