The argv method of the Sys module, which can receive the parameters following the execution script
First, the script is defined as follows:
Import Sys
Print (SYS.ARGV)
Print (Sys.argv[0])
Print (sys.argv[1])
Print (sys.argv[2])
Execute script:
[Email protected] ~]# Python3 aa.py a B
[' aa.py ', ' A ', ' B ', ' C ']//corresponds to print (SYS.ARGV)
aa.py//corresponds to print (sys.argv[0])
A//corresponds to print (sys.argv[1])
b//corresponds to print (sys.argv[2])
The role of the Shutil module: Advanced files, folders, compression package processing module
1, copy of the contents of the file, using the Copyfileobj method
Import Shutil
F1 = open ("passwd", "R", encoding= "Utf-8")
F2 = open ("Passwd.bak", "W", encoding= "Utf-8")
Shutil.copyfileobj (F1,F2)
There is one more simple way:
>>> Import Shutil
>>> shutil.copyfile ("passwd", "passwd.old")//The file can be copied directly using the CopyFile method
' Passwd.old '
2, copy only the permissions of the file, the rest of the properties are not copied, provided that there must be a target file, if the target file does not exist the error
[email protected] ~]# ll passwd
-rwxr-xr-x. 1 xiaoming xiaoming 1576 March 14:52 passwd
[[email protected] ~]# ll Passwd.bak//This file content is empty
-rw-r--r--. 1 root root 0 March 14:56 Passwd.bak
>>> Import Shutil
>>> Shutil.copymode ("passwd", "Passwd.bak")//copymode Copy the permissions of the file
To see the properties of two files:
[email protected] ~]# ll passwd
-rwxr-xr-x. 1 xiaoming xiaoming 1576 March 14:52 passwd
[[email protected] ~]# ll passwd.bak//file contents are empty, but only permissions have changed
-rwxr-xr-x. 1 root root 0 March 14:56 Passwd.bak
3, copy all the state information of the file, do not copy the contents of the file
>>> Import Shutil
>>> shutil.copystat ("passwd", "Passwd.bak")
4, copy files and permissions, but do not copy the owner and the group
>>> shutil.copy ("passwd", "Passwd.bak")
' Passwd.bak '
5. Copy the entire directory tree
>>> Import Shutil
>>> Shutil.copytree ("/etc", "/etc_new")
Delete Entire Directory tree
>>> shutil.rmtree ("/etc_new")
6, the Movement of files
>>> Import Shutil
>>> shutil.move ("passwd", "/tmp")
'/TMP/PASSWD '
7, the packaging of documents
>>> shutil.make_archive ("Test", "Gztar", "/etc")//"Test" is a packaged name, you can specify a path, "Gztar" is a package type, you can also write a zip; "etc" For programs that need to be packaged
'/root/test.tar.gz '
File packaging can also be used in a different way
Import Tarfile
tar = Tarfile.open ("XXX.tar.gz", "w")//"XXX.tar.gz" the name of the open file, which is the TAR package name
Tar.add ("/etc/passwd", Arcname= "passwd")//Add files that need to be compressed
Tar.add ("/etc/shadow", Arcname= "shadow")
Tar.close ()
With compression, then unzip it?
tar = Tarfile.open ("XXX.tar.gz", "R")
Tar.extractall ("/usr/local")//tar.extractall () to set the decompression path
Tar.close ()
Python3 Shutil Module