Some common file and directory operations of the Python OS module

Source: Internet
Author: User

Turn from: Geek College

Python's OS modules encapsulate common file and directory operations, and this article lists only a few common methods and more ways to view official documents.

The following are some common uses:

Method Description
Os.mkdir Create a Directory
Os.rmdir Delete Directory
Os.rename Renaming
Os.remove deleting files
Os.getcwd Get Current work Path
Os.walk Traverse Directory
Os.path.join Connection directory and file name
Os.path.split Split file name and directory
Os.path.abspath Get absolute path
Os.path.dirname Get path
Os.path.basename Get the file name or folder name
Os.path.splitext Detach file name and extension
Os.path.isfile Determines whether the given path is a file
Os.path.isdir Determine if the given path is a directory
Example

The following example of the directory structure for reference, working directory is /Users/ethan/coding/python .

Users/ethan└── coding    └── python        ├── hello.py    - 文件        └── web         - 目录

Take a look at the example:

    • Os.path.abspath: Gets the absolute path to the file or directory
1 $ pwd2/users/ethan/coding/python3 $ python4>>>ImportOs#Remember to import the OS module5>>> Os.path.abspath ('hello.py')6 '/users/ethan/coding/python/hello.py'7>>> Os.path.abspath ('Web')8 '/users/ethan/coding/python/web'9>>> Os.path.abspath ('.')#absolute path to the current directoryTen '/users/ethan/coding/python'
    • Os.path.dirname: Get the path to a file or folder
1>>> Os.path.dirname ('/users/ethan/coding/python/hello.py')2 '/users/ethan/coding/python'3>>> Os.path.dirname ('/users/ethan/coding/python/')4 '/users/ethan/coding/python'5>>> Os.path.dirname ('/users/ethan/coding/python')6 '/users/ethan/coding'
    • Os.path.basename: Gets the file name or folder name
1>>> Os.path.basename ('/users/ethan/coding/python/hello.py')2 'hello.py'3>>> Os.path.basename ('/users/ethan/coding/python/')4 "'5>>> Os.path.basename ('/users/ethan/coding/python')6 'python'
    • Os.path.splitext: Detach file name and extension
1>>> Os.path.splitext ('/users/ethan/coding/python/hello.py')2('/users/ethan/coding/python/hello','. PY')3>>> Os.path.splitext ('/users/ethan/coding/python')4('/users/ethan/coding/python',"')5>>> Os.path.splitext ('/users/ethan/coding/python/')6('/users/ethan/coding/python/',"')
    • Os.path.split: Separating directory from file name
1>>> Os.path.split ('/users/ethan/coding/python/hello.py')2('/users/ethan/coding/python','hello.py')3>>> Os.path.split ('/users/ethan/coding/python/')4('/users/ethan/coding/python',"')5>>> Os.path.split ('/users/ethan/coding/python')6('/users/ethan/coding','python')
    • Os.path.isfile/os.path.isdir
1>>> Os.path.isfile ('/users/ethan/coding/python/hello.py')2 True3>>> Os.path.isdir ('/users/ethan/coding/python/')4 True5>>> Os.path.isdir ('/users/ethan/coding/python')6 True7>>> Os.path.isdir ('/users/ethan/coding/python/hello.py')8False
    • Os.walk

Os.walk is a common module for traversing a directory that returns a Ganso with 3 elements: (Dirpath, Dirnames, filenames). Dirpath returns all the absolute paths in the directory as string strings; Dirnames returns the folder name under each absolute path in list form, Filesnames returns all file names under that path as a list.

1>>> forRoot, dirs, filesinchOs.walk ('/users/ethan/coding'):2...PrintRoot3...Printdirs4...PrintFiles5 ...6/users/ethan/Coding7['python']8 []9/users/ethan/coding/pythonTen['web2'] One['hello.py'] A/users/ethan/coding/python/web2 - [] -[]

 

Some common file and directory operations of the Python OS module

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.