Python common modules

Source: Internet
Author: User

############# #常用模块 ################

OS Module

Provides methods for accessing the specific familiarity of the operating system

provides a package for the Platform Module (for Windows, packaging for Mac, etc.)

Os.environ # #对环境变量的操作

OS.GETCWD () # #获取当前的工作目录

Os.access (path,mode) # #判断一个文件或者目录是否具有指定的权限

Optional values for the mode parameter: Rok,wok, and X_OK

For example:

In [7]: os.access ('/etc ', OS. R_OK)

OUT[7]: True

In [8]: os.access ('/etc ', OS. W_OK)

OUT[8]: True

In [9]: os.access ('/etc ', OS. X_OK)

OUT[9]: True

Os.stat () is equivalent to the stat command under Linux # #查询文件和文件系统的详细信息

Os.listdir () # #列出给定目录的内容

Os.mkdir (path) # #创建目录

Os.mkdirs (Path) # #创建目录树, equivalent to mkdir-p operation

Execute Linux commands using python

Os.system # #没有返回值, the output is produced by side effects

Os.popen # #返回一个文件对象

OS.POENP2 # #返回两个文件对象, one is stdin, one is stdout

Os.popen3 # #返回三个文件对象: Stdin.stdout,stderr

Os.path is a sub-module of the OS, primarily parsing, creating, testing, and

some of his operations encapsulate the path operations of different platforms.

Path Resolution :

Path.split ('/tem/test/ab ')

Path.basename ('/tem/test/ab ')

Path.dirname ('/tem/test/ab ')

650) this.width=650; "title=" screenshot from 2017-07-17 18-02-36.png "src=" https://s3.51cto.com/wyfs02/M00/9C/2A/ Wkiol1ls5rqswpeiaabwvabw8u4006.png-wh_500x0-wm_3-wmp_4-s_4112647973.png "alt=" Wkiol1ls5rqswpeiaabwvabw8u4006.png-wh_50 "/>

Path.join (' A ', ' B ', ' C ')

Path.abspath ('. ')

Path.splitext (' aaa.tar.gz ')

650) this.width=650; "title=" screenshot from 2017-07-17 18-04-55.png "src=" https://s3.51cto.com/wyfs02/M01/9C/2A/ Wkiom1ls5txy44eoaabrrt88clw607.png-wh_500x0-wm_3-wmp_4-s_3068993344.png "alt=" Wkiom1ls5txy44eoaabrrt88clw607.png-wh_50 "/>

Os.path.getatime (' filename ') # #最后一次访问文件或目录的时间

Os.path.getctime (' filename ') # #最后一次文件改变或目录改变的时间

Os.path.getmtime (' filename ') # #最后一次文件或目录修改的时间

Os.path.getsize (' filename ') # #文件的大小

650) this.width=650; "title=" screenshot from 2017-07-17 18-14-48.png "src=" https://s4.51cto.com/wyfs02/M01/9C/2A/ Wkiol1ls5usaapzmaab10c5p89k960.png-wh_500x0-wm_3-wmp_4-s_2077628265.png "alt=" Wkiol1ls5usaapzmaab10c5p89k960.png-wh_50 "/>

file test related :

Os.path.isabs

Os.path.isdir # #判断是否是一个目录

Os.path.isfile # #判断是否是一个文件

Os.path.islink # #判断是否是一个链接

Exercise : Find all files in the user's given directory that end with. log

Import OS

Dir=raw_input (' directory ')

Filelist=os.listdir (dir)

For file in FileList:

If File.endswith ('. log '):

With open (' logfile ', ' A + ') as F:

A=os.path.join (Dir,file)

F.write (a)

F.write (' \ n ')

Test:

650) this.width=650; "title=" screenshot from 2017-07-17 18-28-34.png "style=" Float:none; "src=" https://s3.51cto.com/ Wyfs02/m01/9c/2a/wkiol1ls5xdtf_xtaaa2rrixsa4213.png-wh_500x0-wm_3-wmp_4-s_3461114784.png "alt=" wKioL1ls5xDTf_ Xtaaa2rrixsa4213.png-wh_50 "/>

650) this.width=650; "title=" screenshot from 2017-07-17 18-28-40.png "style=" Float:none; "src=" https://s3.51cto.com/ Wyfs02/m02/9c/2a/wkiom1ls5xdzwo0uaaa5pmpcq-e413.png-wh_500x0-wm_3-wmp_4-s_766184655.png "alt=" Wkiom1ls5xdzwo0uaaa5pmpcq-e413.png-wh_50 "/>


Time Module

Three ways to represent time in Python:

1. Timestamp time.time ()

2. Formatted time string

3. Tuple (total of 9 elements) Time.localtime ()

Time.mktime (t) convert tuple format time to timestamp

Time.localtime () convert timestamp to tuple format time

Time.sleep (secs)

Time.ctime ([secs]) converts a timestamp to a string format display

Time.strftime (format[,t]) converts tuple format time to string display

Time.strptime (String[,format]) Convert string display to tuple format time

650) this.width=650; "title=" screenshot from 2017-07-17 18-40-34.png "style=" Float:none; "src=" https://s4.51cto.com/ Wyfs02/m00/9c/2b/wkiol1ls5zia4p8haab3vjxbjhg582.png-wh_500x0-wm_3-wmp_4-s_471178876.png "alt=" Wkiol1ls5zia4p8haab3vjxbjhg582.png-wh_50 "/>

650) this.width=650; "title=" screenshot from 2017-07-17 18-44-33.png "style=" Float:none; "src=" https://s2.51cto.com/ Wyfs02/m02/9c/2b/wkiom1ls5ziixtdwaabjqzybd3q542.png-wh_500x0-wm_3-wmp_4-s_2987716999.png "alt=" Wkiom1ls5ziixtdwaabjqzybd3q542.png-wh_50 "/>



Python common 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.