Python third-party library series 23--Path Library

Source: Internet
Author: User

First, remove the current path:

Base_dir1 = Os.path.dirname (Os.path.dirname (__file__))
Base_dir2 = Os.path.dirname (Os.path.abspath (__file__))
# __file__: Refers to the directory address where this file resides (including the file name) but is related to the directory where the Python command is executed (relative address).
# dirname (__file__): The top level of the file.
# Abspath (__file__): The absolute path where the file is located (including the file name in this address).
Second, determine whether the path exists

(1) Determine if the folder exists

Os.path.exists (PATH)
(2) Determine whether the file exists

Os.path.isfile (' a.txt ') #如果不存在就返回False 
os.path.exists (directory) #如果目录不存在就返回False
(3) Create a folder

Os.makedirs (path) #多层创建目录
os.mkdir (path) #创建目录


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.