Python standard library 04 file management (partial OS package, Shutil package)

Source: Internet
Author: User
Tags symlink

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Under the operating system, the user can manage the files through the commands of the operating system, referring to the Linux file management related commands. the P Ython Standard library allows us to manage files from within Python. For the same purpose, we have two avenues. While Python calls the standard library in a way that is not as straightforward as operating system commands, it has its own advantages. You can use the Python language and play other Python tools to form a portfolio of file management functions. Python or Shell? This is the choice left to the user. In this article, we try to compare the similarities between the two functions.

This article is based on Linux file management background knowledge

OS Package

The OS package includes a variety of functions to implement many of the functions of the operating system. This bag is very complicated. Some of the commands for the OS package are for file management. We list the most common ones here:

mkdir (path)

Creates a new directory, path is a string that represents the paths to the new directory. Equivalent to $mkdir command

RmDir (path)

Delete the empty directory, path is a string that represents the path of the directory you want to delete. Equivalent to $rmdir command

Listdir (path)

Returns all files in the directory. Equivalent to $ls command.

Remove (path)

Delete the file that the path points to.

Rename (src, DST)

Rename the file, src and DST are two paths, each representing the path before and after the rename.

chmod (path, mode)

Change the permissions of the file that the path points to . Equivalent to $chmod command.

Chown (path, UID, GID)

Change the owner and owning group of the file that the path points to . Equivalent to $chown command.

Stat (path)

View additional information for the file pointed to by Path , equivalent to the $ls-l command.

Symlink (src, DST)

Create a soft link for the file DST , SRC is the path to the soft link file. Equivalent to $ln-s command.

GETCWD ()

Querying the current working path (CWD, working directory) is equivalent to $pwd command.

For example, we're going to create a new directory

Import osos.mkdir ('/home/vamei/new')   

Shutil Bag

Copy (src, DST)

Copy files from Src to DST. Equivalent to $CP command.

Move (src, DST)

Move files from Src to DST. Equivalent to $MV command.

For example, we want to copy the file A.txt:

Import shutilshutil.copy ('a.txt'b.txt')     

For further details, please refer to the official documentation OS, Shutil.

In conjunction with this chapter and previous content, we have made Python a powerful tool for file management.

Summary

OS pack: RmDir, mkdir, Listdir, remove, rename, chmod, Chown, stat, symlink

Shutil Package: Copy, move

Python standard library 04 file management (partial OS package, Shutil package)

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.