Python Os.path module Common methods __python

Source: Internet
Author: User

Os.path module is mainly used for file property acquisition, often used in programming, the following are several common methods of this module. More ways to view official documents: http://docs.python.org/library/os.path.html

1.os.path.abspath (PATH)   Returns the absolute path normalization.     >>> os.path.abspath (' test.csv ')   ' c:\\python25\\test.csv '     >> > os.path.abspath (' c:\\test.csv ')   ' c:\\test.csv '     >>> os.path.abspath (' ... /csv\\test.csv ')   ' c:\\csv\\test.csv '     2.os.path.split (path)   divides the path into directory and filename two-tuple returns.     >>> os.path.split (' C:\\csv\\test.csv ')   (' C:\\csv ',  ' test.csv ')   > >> os.path.split (' c:\\csv\\ ')   (' c:\\csv ',  ')     3.os.path.dirname (path)   Returns the directory of path. is actually the first element of Os.path.split (path).     >>> os.path.dirname (' c:\\csv\test.csv ')   ' c:\\ '   >>>  Os.path.dirname (' c:\\csv ')   ' c:\\ '     4.os.path.basename (path)   Returns the filename of the last path. How the path ends with/or, the null value is returned. The second element of Os.path.split (path).     >>> os.path.basename (' c:\\test.csv ')   ' test.csv '   >>> OS.PATh.basename (' c:\\csv ')   ' CSV '   (here CSV is treated as filename)   >>> os.path.basename (' c:\\csv\\ ')   '     5.os.path.commonprefix (list)   Returns the longest path shared by all paths in the list.     such as:  >>> os.path.commonprefix (['/home/td ', '/home/td/ff ', '/home/td/fff '])   '/ Home/td '     6.os.path.exists (path)   Returns true if path exists, false if path does not exist.     >>> os.path.exists (' c:\\ ')   true  >>> os.path.exists (' c:\\csv\ \test.csv ')   false    7.os.path.isabs (PATH)   Returns True if path is an absolute path.     8.os.path.isfile (path)   Returns True if path is a file that exists. otherwise returns false.     >>> os.path.isfile (' C:\\Boot.ini ')   true  >>> os.path.isfile (' C:\\csv\\test.csv ')   false  >>> os.path.isfile (' c:\\csv\\ ')   false 

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.