Python standard library Os.path packages, Glob package usages _python

Source: Internet
Author: User
Tags glob

Os.path Bag

Os.path packages are primarily used to handle string paths, such as '/home/zikong/doc/file.doc ', to extract useful information.

Copy Code code as follows:

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

Print (Os.path.basename (path)) # The file name contained in the query path
Print (Os.path.dirname (path)) # The directory contained in the query path

info = os.path.split (path) # divides paths into file names and directories two parts, and returns them in a table
path2 = Os.path.join (' \ ', ' home ', ' Zikong ', ' Doc ', ' File.doc ') #使用目录名和文件名构成一个路径字符串

P_list = [path, path2]
Print (Os.path.commonprefix (p_list)) # Querying common parts of multiple paths

Os.path.normpath (PATH) # removes redundancy from path paths. For example, '/home/vamei/. /.' be converted to '/home '
#os. Path can also query for information about the file (metadata). The information about the file is not stored inside the file, but by the operating system
#维护的, some information about the file (such as file type, size, 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 File size
Print (Os.path.getatime (path)) # Query the time the file was last read
Print (Os.path.getmtime (path)) # Query when the file was last modified

Print (Os.path.isfile (path)) # Path points to regular files
Print (Os.path.isdir (path)) # path point to directory file

Glob bag

Glob is a file-operation-related module with Python's own it's not much, it can look for files that fit your purpose, similar to a file search under Windows, and it also supports wildcards,,?, [] These three wildcards, representing 0 or more characters? Represents a character, [] Matches the specified range of characters, such as [0-9] matching digits.

Glob method: Returns a list of all matching file paths that require a parameter to specify a matching path string (This string can be either an absolute or a relative), such as:

Copy Code code as follows:

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

Example

Comprehensive utilization of two packages of a similar to Linux under the LS function:

Copy Code code as follows:

#coding = UTF8
Import glob
Import os.path
Path = '/users/zik Ong/pictures '
def ls (path):
        #codinf = UTF8
         print "--name--  --type-- --size-- --atime-- --mtime--"
& nbsp;       Path = path + '/* '
        filelist = Glob.glob (path)
        for filepath in filelist:
                 out = '%s %s %s %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)

Attention

Copy Code code as follows:

#coding =utf

is to enable Python to display Chinese

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.