Python Directory Operations

Source: Internet
Author: User

In Python, the file operations are mainly from the OS module, the main methods are as follows:

Os.listdir (dirname): List directories and files under DirName

OS.GETCWD (): Get current working directory

Os.curdir: Returns the current directory ('. ')

Os.chdir (dirname): Changing the working directory to DirName

Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false

Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false

Os.path.exists (name): Determine if there is a file or directory name

Os.path.getsize (name): Get file size, if name is directory return 0L

Os.path.abspath (name): Get absolute path

Os.path.normpath (PATH): Canonical path string form

Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)

Os.path.splitext (): Detach file name and extension

Os.path.join (path,name): Connection directory with file name or directory

Os.path.basename (PATH): Return file name

Os.path.dirname (PATH): Return file path

Os.remove (dir) #dir为要删除的文件夹或者文件路径

Os.rmdir (Path) #path要删除的目录的路径. It should be explained that the directory deleted using Os.rmdir must be an empty directory, or the function will fail.


This article is from the "My Ops Time" blog, so be sure to keep this source http://aaronsa.blog.51cto.com/5157083/1783322

Python Directory Operations

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.