Python Learning note 24 (path with file (Os.path package, Glob package))

Source: Internet
Author: User
Tags glob

Os.path module is mainly used for file property acquisition, which is often used in programming, the following are several common methods of the module.

>>>ImportOs.path>>> Path ='/home/ethon/doc/file.txt'>>> Os.path.abspath (PATH)#returns the absolute path normalized by path'C:\\home\\ethon\\doc\\file.txt'>>>os.path.split (path) # divides path into directory and file name two tuples return  ('/home/ethon/doc','file.txt')>>>os.path.dirname (path) # Returns the directory of path, which is actually the first element of Os.path.split (path)  '/home/ethon/doc'>>>os.path.basename (path) # Returns the last file name of path and returns null if path ends with/or \. The second element of Os.path.split (path). 'file.txt'>>> >>> Os.path.commonprefix (['/home/ethon','/home/ethon/doc','/home/ethon/doc/ff'] # returns the longest path common to all paths in the list  '/home/ethon

Os.path can also query information about the file (metadata)

Import'/home/ethon/doc/file.txt'os.path.exists (path)   # query file exists os.path.getsize (path)   #  query File size os.path.getatime (path)  # query file Last read time os.path.getmtime (path)  #  query file Last modified time os.path.isfile (path)    #  does the path point to regular file os.path.isdir (path)     # whether the path points to a directory file
Glob Bag
" " In python, the Glob module is used to find matching files in a lookup condition that requires a matching rule in the Unix shell: *    :   match all?    :   match one character *. *  :   Matches such as: [Hello.txt,cat.xls,xxx234s.doc]?. *  :   matches such as: [1.txt,h.py]?. GIF:   matches such as: [X.gif,2.gif] If there is no match, Glob.glob (path) will return an empty list:[""

Demo

Import glob>>> glob.glob ('c:\\pict\\*.*') ['c:\\pict \\Model.txt'c:\\pict\\output.txt'c:\\pict\\ Output.xls'c:\\pict\\pict.exe'c:\\pict\\ picthelp.htm']

Python Learning note 24 (Path and file (Os.path package, Glob package))

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.