Python standard library OS. path package and glob package use instance

Source: Internet
Author: User
Tags glob
This article mainly introduces the Python standard library OS. path package and glob package usage examples. This article provides the code directly. the code contains detailed comments. For more information, see OS. path package

The OS. path package is mainly used to process string paths, such as '/home/zikong/doc/file.doc' to extract useful information.

The code is as follows:


Import OS. path
Path = '/home/zikong/doc/file.doc'

Print (OS. path. basename (path) # query the file name contained in the path
Print (OS. path. dirname (path) # query the directories contained in the path

Info = OS. path. split (path) # split the path into two parts: file name and directory.
Path2 = OS. path. join ('\', 'home', 'zikong ', 'Doc', 'file.doc') # 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

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 by the operating system.
# Maintenance: information about files (such as file type, size, and modification time ).

Import OS. path
Path = '/home/vamei/doc/file.txt'

Print (OS. path. exists (path) # Query whether a file exists

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) # whether the path points to a regular file
Print (OS. path. isdir (path) # whether the path points to a directory file

Glob package

Glob is a python file operation module, with a small amount of content. it can be used to find files that meet your own purposes, similar to File Search in Windows, wildcard characters ,,?, [] These three wildcards represent 0 or multiple characters ,? Represents a character. [] matches a character in the specified range, for example, [0-9] matches a number.

Glob method: returns the list of all matched file paths. this method requires a parameter to specify the matched path string (this string can be an absolute or relative path). For example:

The code is as follows:


Import glob
Glob. glob ("/home/zikong/doc/*. doc ")
/Home/zikong/doc/file1.doc/home/zikong/doc/file2.doc

Example

Using two packages to write a function similar to the ls function in linux:

The code is as follows:


# Coding = utf8
Import glob
Import OS. path
Path = '/Users/zikong/pictures'
Def ls (path ):
# Codinf = utf8
Print "-- name -- type -- size -- atime -- mtime --"
Path = path + '/*'
Filelist = glob. glob (path)
For filepath in filelist:
Out = '% s % s' % (filepath. split ('/') [4], OS. path. isfile (filepath), OS. path. getsize (filepath), OS. path. getatime (filepath), OS. path. getmtime (filepath ))
Print out

Ls (path)

Note:

The code is as follows:


# Coding = utf

To enable python to display Chinese characters

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.