Chapter 9 Practical Project Reference Answers for Quick Start python Programming

Keywords: Python MySQL CentOS yum

This chapter introduces the use of shutil and zipfile modules. Let's first understand these two modules.

I. shutil module

The shutil module is mainly used to process files or folders, including copying, moving, renaming and deleting files. In the shutil module, there are mainly the following functions:

1. Copy files and folders

Shutil module provides two functions: shutil.copy() and shutil.copytree()

The grammatical format of shutil.copy:

copy(src, dst)

Effect:

Copy the files at src to the dst path, where src and dst are both string-like paths. If the dst is a file name, it will be the new name of the copied file, which is equivalent to copying the original file to the new path and renaming it.

Give an example:

Copy / etc/my.cnf to / root/mysql

In [12]: import shutil

In [13]: shutil.copy('/etc/my.cnf','/root/mysql/')
Out[13]: '/root/mysql/my.cnf'

In [15]: ll /root/mysql/
total 4
-rw-r--r--. 1 root 960 Apr 22 16:57 my.cnf

Copy / etc/my.cnf to / root/mysql and rename it mysql.cnf

In [16]: shutil.copy('/etc/my.cnf','/root/mysql/mysql.cnf')
Out[16]: '/root/mysql/mysql.cnf'

In [17]: ll /root/mysql/
total 8
-rw-r--r--. 1 root 960 Apr 22 16:57 my.cnf
-rw-r--r--. 1 root 960 Apr 22 17:00 mysql.cnf

 

The grammatical format of shutil.copytree:

copytree(src, dst)

Effect:

Copy the entire folder. Copy the folder at src, including all its files and subfolders, to the folder at path dst. Returns a string of newly copied folder paths.

Give an example:

In [20]: shutil.copytree('/etc/yum.repos.d','/root/repo.back')
Out[20]: '/root/repo.back'

In [21]: ll /root/drwxr-xr-x. 2 root 4096 Apr 23  2017 repo.back/
drwxr-xr-x. 2 root 4096 Apr 23  2017 repo.bak/

Note: dst must be a system without directories, otherwise it will report errors:

In [20]: shutil.copytree('/etc/yum.repos.d','/root/repo.bak')
FileExistsError: [Errno 17] File exists: '/root/repo.bak'

2. Moving and renaming files and folders

shutil.move()

Grammatical Format:

move(stc,dst)

Effect:

Move the folder at path stc to path dst and return the string of absolute path at the new location.

Give an example:

Move a.txt from / root / directory to / root/test / directory

In [26]: shutil.move('/root/a.txt','/root/test/')
Out[26]: '/root/test/a.txt'

In [27]: ll /root/test
total 4
-rw-r--r--. 1 root 6 Apr 22 17:30 a.txt

Be careful:

If the dst points to a folder, the src file will move to the dst and keep the original file name, provided that the dst must be a directory that already exists in the system.

If a file with the same name already exists in the target file, it should be noted that it will be overwritten.

3. Delete files and folders

In the os module:

os.remove(path) can delete a file

os.rmdir(path) can delete an empty folder.

In shutil module:

shutil.rmtree(path) can delete a folder and all its contents.

Grammatical Format:

os.rmdir(path)
shutil.rmtree(path)

Give an example:

 1 In [34]: os.remove('/root/test/a.txt')
 2 
 3 In [35]: ll /root/test/
 4 total 0
 5 
 6 In [36]: shutil.move('/root/CentOS-Base.repo','/root/test/')
 7 Out[36]: '/root/test/CentOS-Base.repo'
 8 
 9 In [37]: ll test
10 total 4
11 -rw-r--r--. 1 root 2573 Apr 23  2017 CentOS-Base.repo
12 
13 In [38]: shutil.rmtree('/root/test')
14 
15 In [39]: ll
16 total 12
17 -rw-------. 1 root 1468 Apr 16 21:03 anaconda-ks.cfg
18 drwxr-xr-x. 2 root   57 Apr 23  2017 download/
19 drwxr-xr-x. 2 root   37 Apr 22 17:00 mysql/
20 drwxr-xr-x. 6 root   95 Apr 16 21:58 py34/
21 drwxr-xr-x. 3 root   18 Apr 16 23:01 python/
22 drwxr-xr-x. 2 root 4096 Apr 23  2017 repo.back/
23 drwxr-xr-x. 2 root 4096 Apr 23  2017 repo.bak/

Be careful:

The above deletions are permanent deletions. For security, it's best to use send2trash third-party module, which will put deleted files into the recycle bin. This module has been integrated in Python 3.

send2trash usage:

import send2trash
send2trash(path)

II. Traversing the Catalog Tree

Processing files, especially batch operations, has to traverse directories. In python, the os.walk() function in the OS module can be achieved.

This function recursively traverses the specified directory and subdirectories, returning a three-tuple message: current directory name, subdirectory name, file name, excluding. and..

Common usage:

#!/usr/bin/env python3.4
#coding:utf-8
import os
for foldName,subfolders,filenames in os.walk('/root/'):
    print('The current folder is: ' + foldName)
    for subfolder in subfolders:
        print('subfolder of ' + foldName + ':' + subfolder)
    for filename in filenames:
        print('file inside ' + foldName + ':' + filename)
    print('')

3. Reference Answers for Practical Projects

 1 #!/usr/bin/env python3.4
 2 # coding:utf-8
 3 import os
 4 import shutil
 5 import send2trash
 6 
 7 # 9.8.1
 8 # Copy the specified format file to the specified directory. The following procedure is to/etc What's in the catalogue.conf Copy files to/root/test/In the catalogue.
 9 src = '/etc/'
10 dst = '/root/test/'
11 ftype = '.conf'
12 count = 0
13 for filename in os.listdir(src):
14     if filename.endswith(ftype):
15         shutil.copy(src + filename,dst)
16         count += 1
17         print('file ' + src + filename + '\t Copied to---> ' + dst + ' Under the catalogue')
18 print("All in this directory " + ftype + "The file has been copied to" + dst + "Under the catalogue")
19 print('Copy it altogether ' + str(count) + ' Five files')
20 
21 # 9.8.2
22 # Search for directories greater than 100 M Documents,Print it out and delete it
23 # You can manually create an empty file of a specified size for experimentation
24 # dd if=/dev/zero of=hello.txt bs=100M count=1
25 for foldname,subfolders,filenames in os.walk(dst):
26     for files in filenames:
27         if os.path.getsize(dst + files) / 1024 /1024 > 100:
28             print('More than 100 M Documents include:' + files + ' ' +  str(os.path.getsize(dst + files) / 1024 / 1024) +'Mb')
29             send2trash.send2trash(dst + files)
30 
31 # 9.8.3
32 # 

Posted by BrianM on Sat, 06 Jul 2019 15:49:07 -0700