Python common built-in modules

Source: Internet
Author: User

Common built-in modules in Python (datetime,logging,os,command)

Import module in Python using keyword import

1, if you are importing files locally, use the following directly:Import filename
2, if the import is a package, the package below must be a__init__.pyFiles can be imported, otherwise
Error, only have the__init__.pyFilePythonThe parser will take this directory as a package.
Common formats for commonly used import modules:
form XXX Import xxx
Import xxx
When importing, the suffix of. PY is omitted directly, if it is a multi-level package, or if you want to import the letter inside the package
Can be imported using from , for example:
From AAA import BBB
Import OS
Explanation: The first example is to import the BBB module under the AAA package or to import the BBB under the AAA file
Class or function
The second import is the direct import of the system module OS module
Import Ling.test as AAA
Print (' aaaaaaaaaaaaaaaaaaaaaa ')
Aaa.hello ()
Print (' aaaaaaaaaaaaaaaaaaaaaa ')

In the daily development work, we have to write a lot ofPythonCode, if both are written in a file
Will cause code to be particularly difficult to maintain, in order to expand the maintainability of the code, we write the letter in different text
files, so that each file contains fewer files and is more logically clear. InPythonin which we
The files that are created are basically. pyThe end, the one. pyFile is called a module.
In order to facilitate the management of convenient management module,PythonThe bag was also cited in thePackage) This concept. Every
There's a bag underneath.__init__.pyFile, this file is a must exist, otherwise,PythonJust put
This directory is considered a normal directory, not a package. __init__.py can be empty files, or there can be
Python code, because __init__.py itself is a module, for example: theTest directory below has
__init__.py, aaa.py,bbb.py three files, as shown:
[Email protected] ~]# tree test
Test
├── aaa.py
├── bbb.py
└── __init__.py
0 directories, 3 files

Use of the DateTime module

Get current time: Now_time

Convert time to string using strftime

Convert string to TIME use: Strptime

Calculate the time of yesterday or tomorrow, with the current time for reference, using the keyword: timedelta

Format parameters:
%Y 10 year with century part
%m the month represented by decimal
The day ordinal of a month in%d decimal notation
%H hours of the year
%M minutes in a 10 o'clock-hour representation
%s number of seconds in decimal
%c Standard Time, such as:04/25/17 14:35:14 similar to this form
These parameters are the most commonly used time we use.

Conversion of timestamps:

Time stamp:Time stamp refers to the GMT1970 year 01
month 01 day 00 Span class= "Fontstyle0" when 00 00 s ( Beijing time 1970 year 01 month 01 day 08 00 00 seconds ) to now.

use keyword: fromtimestamp Convert

Python common built-in modules

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.