Introduction to the Os.path module in Python

Source: Internet
Author: User

1. Get the current working directory in Python

CurDir = OS.GETCWD ()

OS.GETCWD () returns the directory where the command was executed, not the directory in which the script itself resides

2, Os.path

  1. Os.path.abspath (path) #返回绝对路径
  2. Os.path.split (path) #将path分割成目录和文件名二元组返回
  3. Os.path.dirname (path) #返回path的目录. is actually the first element of Os.path.split (path)
  4. Os.path.basename (path) #返回path最后的文件名
  5. Os.path.exists (path) #如果path存在, returns True if path does not exist, returns false
  6. Os.path.isabs (path) #如果path是绝对路径, returns True
  7. Os.path.isfile (path) #如果path是一个存在的文件, returns True. Otherwise returns false
  8. Os.path.isdir (path) #如果path是一个存在的目录, returns True. Otherwise returns false
  9. Os.path.getatime (path) #返回path所指向的文件或者目录的最后存取时间
  10. Os.path.getmtime (path) #返回path所指向的文件或者目录的最后修改时间
  11. Os.path.curdir #返回当前目录 (relative path)
  12. Os.path.pardir #返回父目录
  13. S.path.join (path1[, path2[, ...])  #将多个路径组合后返回, the parameters before the first absolute path are ignored.
  14. >>> os.path.join (' c:\\ ', ' csv ', ' test.csv ')
  15. ' C:\\csv\\test.csv '
  16. >>> os.path.join (' windows\temp ', ' c:\\ ', ' csv', ' test.csv ')
  17. ' C:\\csv\\test.csv '
  18. >>> os.path.join ('/home/aa ','/home/aa/bb ','/home/aa/bb/c ')
  19. '/home/aa/bb/c '

Introduction to Os.path modules in Python

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.