Python standard library 03 path and file (OS. Path package, glob package)

Source: Internet
Author: User
Tags glob

Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!

 

1. Introduction to paths and files

See Linux File System

Http://www.cnblogs.com/vamei/archive/2012/09/09/2676792.html

 

2. OS. Path package

The OS. Path package mainly processes path strings, such as '/home/vamei/doc/file.txt', to extract useful information.

Import OS. pathpath = '/home/vamei/doc/file.txt' print (OS. path. basename (PATH) # query the file name print (OS. path. dirname (PATH) # The query path contains the directory info = OS. path. split (PATH) # split the path into two parts: File Name and directory. Put them in a table and return path2 = OS. path. join ('/', 'home', 'vamei', 'Doc', 'file1.txt ') # use the directory name and file name to form a path string p_list = [path, path2] print (OS. path. commonprefix (p_list) # query the common parts of multiple paths

In addition, there are the following methods:

OS. Path. normpath (Path) # Remove redundancy in the path. For example, '/home/vamei/...' is converted to '/home'

 

OS. path can also be used to query information about a file (metadata ). The file information is not stored in the file, but maintained by the operating system. Some information about the file (such as the file type, size, modification time ).

Import OS. path = '/home/vamei/doc/file.txt' print (OS. path. exists (PATH) # query whether the file has print (OS. path. getsize (PATH) # query the file size print (OS. path. getatime (PATH) # query the last file read time print (OS. path. getmtime (PATH) # query the last file modification time print (OS. path. isfile (PATH) # specifies whether the path points to the regular file print (OS. path. isdir (PATH) # Whether the path points to a directory file

(In fact, this part is similar to some functions of Linux's LS command)

 

3. glob package

The most common method of a glob package is glob. glob (). The function of this method is similar to that of LS in Linux (see Linux File Management commands). It accepts a file name format expression (filename pattern expression) in Linux ), list all files that match the expression (similar to the regular expression) and put all file names in a table to return. Therefore, glob. glob () is a good way to query files in a directory.

The syntax of this file name expression is different from the regular expression of Python itself (you can check the fnmatch package at the same time. Its function is to check whether a file name conforms to the Linux File Name format expression ). As follows:

Filename pattern expression Python Regular Expression

*.*

? .

[0-9] Same

[A-E] Same

[^ MNP] Same

 

We can use this command to find all the files under/home/vamei:

import globprint(glob.glob('/home/vamei/*'))

 

Summary

File System

OS. Path

Glob. glob

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.