Print output date information in Python

Source: Internet
Author: User

There are several ways to print information about the Linux command line date command in Python:


Method 1: Directly call the Linux command output, also can print the host name;

[email protected] tmp]# cat 1.py

#!/usr/bin/python

Import Os,commands

hostname = commands.getoutput (' hostname ')

Date = Commands.getoutput (' Date ')

Print hostname

Print Date


[[email protected] tmp]# python 1.py

host74

Thursday, May 25, 2017 16:05:16 CST


Method 2: The same as the result of method 1 execution;

[email protected] tmp]# cat 2.py

#!/usr/bin/python

Import OS

Os.system ("date")


[[email protected] tmp]# python 2.py

Thursday, May 25, 2017 16:06:39 CST



Method 3: Using the time Module CTime () display format looks less intuitive;

[email protected] tmp]# cat 2.py

#!/usr/bin/python

Import time

Print Time.ctime ()


[[email protected] tmp]# python 2.py

Thu May 25 16:05:50 2017


Method 4: Using the time module, according to the date of month and day display, more intuitive;

[email protected] tmp]# cat 2.py

#!/usr/bin/python

Import time

Print Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))


[[email protected] tmp]# python 2.py

2017-05-25 16:06:03




This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1929464

Print output date information in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.