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 Python 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 the $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 the $rmdir command
Listdir (path)
Returns all files in the directory. Equivalent to the $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 the $chmod command.
Chown (path, UID, GID)
Change the owner and owning group of the file that the path points to. Equivalent to the $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 the $ln-s command.
GETCWD ()
Query the current working path (CWD, working directory), equivalent to the $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 the $CP command.
Move (src, DST)
Move files from Src to DST. Equivalent to the $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.
Summarize
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)