shutil module - advanced file, folder, compressed package processing module

Keywords: Python encoding codec

  • shutil.copyfileobj(fsrc, fdst[, length]) copies the contents of the file to another file, and the length is the size of each copy
guessage.py Chinese content
"""
//Age guessing game:
//Users are allowed to guess three times at most. After three guesses, they will continue to play. If you enter Y, you can continue to guess three times. Otherwise, you will exit
"""
age = 23
count = 0
while count < 3:
    try:
        guess_age = int(input("input the age of you think:"))
    except ValueError:
        print("you should input one number!")
        count = count + 1
        continue

    if guess_age > 23:
        print("the age you input is too big!")
    elif guess_age < 23:
        print("the age you input is too small!")
    else:
        print("excellent!you are right!")
        break
    count = count + 1
    while count == 3:
        your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
        if your_choice.lower() == "y":
            count = 0
        elif your_choice.lower() =="n":
            break
        else:
            print("your input is illegal!input again!")
"Correct program running code"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfileobj(open("guessage.py","r",encoding="utf-8"),open("guessage_new.py","w",encoding="utf-8",10))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py

Process finished with exit code 0
"Error message when no coding is added"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfileobj(open("guessage.py","r"),open("guessage_new.py","w"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 5, in <module>
    shutil.copyfileobj(open("guessage.py","r"),open("guessage_new.py","w"))
  File "D:\software2\Python3\install\lib\shutil.py", line 79, in copyfileobj
    buf = fsrc.read(length)
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 29: illegal multibyte sequence

Process finished with exit code 1
guessage_new.py
"""
//Age guessing game:
//Users are allowed to guess three times at most. After three guesses, they will continue to play. If you enter Y, you can continue to guess three times. Otherwise, you will exit
"""
age = 23
count = 0
while count < 3:
    try:
        guess_age = int(input("input the age of you think:"))
    except ValueError:
        print("you should input one number!")
        count = count + 1
        continue

    if guess_age > 23:
        print("the age you input is too big!")
    elif guess_age < 23:
        print("the age you input is too small!")
    else:
        print("excellent!you are right!")
        break
    count = count + 1
    while count == 3:
        your_choice = input("you only have three chances,would you like to continue(Y|y/N|n):")
        if your_choice.lower() == "y":
            count = 0
        elif your_choice.lower() =="n":
            break
        else:
            print("your input is illegal!input again!")
  • shutil.copyfile(src, dst) copy file
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copyfile("guessage.py","guessage_new.py")#Target file does not need to exist
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=7881299347900196, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1557104769, st_mtime=1557104769, st_ctime=1557104769)

Process finished with exit code 0
  • shutil.copymode(src, dst) only copies permissions. Content, group and user remain unchanged
"Program code"
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
shutil.copymode("guessage.py","guessage_new.py")#Target file must exist
#When the target file does not exist
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 5, in <module>
    shutil.copymode("guessage.py","guessage_new.py")
  File "D:\software2\Python3\install\lib\shutil.py", line 144, in copymode
    chmod_func(dst, stat.S_IMODE(st.st_mode))
FileNotFoundError: [WinError 2] The system cannot find the specified file.: 'guessage_new.py'

Process finished with exit code 1
#Create a new file, guissage_new.py, the content is empty, and then run the program
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copymode("guessage.py","guessage_new.py")
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=2533274790397796, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=61, st_atime=1557104202, st_mtime=1557104202, st_ctime=1557104202)

Process finished with exit code 0
  • shutil.copystat(src, dst) only copies state information, including: mode bits, atime, mtime, flags
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copystat("guessage.py","guessage_new.py") #Target file must exist
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=2814749767108452, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=61, st_atime=1555571381, st_mtime=1555571381, st_ctime=1557104576)

Process finished with exit code 0
  • shutil.copy(src, dst) copy files and permissions
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copy("guessage.py","guessage_new.py")#Target file can not exist
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=6473924464346978, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1557104716, st_mtime=1557104716, st_ctime=1557104716)

Process finished with exit code 0
  • shutil.copy2(src, dst) copies files and status information
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.copy2("guessage.py","guessage_new.py")
print("guessage.py",os.stat("guessage.py"))
print("guessage_new.py",os.stat("guessage_new.py"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
guessage.py os.stat_result(st_mode=33206, st_ino=9007199254741804, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1555571381)
guessage_new.py os.stat_result(st_mode=33206, st_ino=7318349394478946, st_dev=3466358229, st_nlink=1, st_uid=0, st_gid=0, st_size=941, st_atime=1555571381, st_mtime=1555571381, st_ctime=1557104918)

Process finished with exit code 0
  • shutil.ignore_patterns(*patterns)
  • shutil.copytree(src, dst, symlinks=False, ignore=None) recursively copies folders
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

# The target directory cannot exist. Note that the parent directory of folder2 directory must have writable permission. ignore means exclude
shutil.copytree('ee', 'new_ee', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
print(os.listdir("ee"))
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['eee']
['eee']

Process finished with exit code 0
  • Shutil.rmtree (path [, ignore error [, onerror]]) recursively deletes files
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

shutil.rmtree('new_ee')
print(os.listdir("ee"))
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
['eee']
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 10, in <module>
    print(os.listdir("new_ee"))
FileNotFoundError: [WinError 3] The system cannot find the specified path.: 'new_ee'

Process finished with exit code 1
  • shutil.move(src, dst) recursively moves files. It is similar to mv command, but it is actually rename.
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os

shutil.move('ee','new_ee')
print(os.listdir("ee"))#After move, the source file does not exist
print(os.listdir("new_ee"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 9, in <module>
    print(os.listdir("ee"))
FileNotFoundError: [WinError 3] The system cannot find the specified path.: 'ee'

Process finished with exit code 1

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
import os
shutil.move('new_ee','mkdir') #Can move to another folder
print(os.listdir("mkdir"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['new_ee']

Process finished with exit code 0
  • Shutil.make'archive (base'name, format,...) creates a compressed package and returns the file path, for example: zip, tar
base_name:  The filename of the compressed package, or the path of the compressed package. If it is just a filename, it will be saved to the current directory, otherwise it will be saved to the specified path,
//For example, data > bak = > save to the current path
//For example, / TMP / data_bak = > save to / tmp/

format:  Type of compression package“ zip", "tar", "bztar","gztar"
root_dir:  Folder path to compress (default current directory)
owner:  User, default current user
group:  Group, default current group
logger:  For logging, usually logging.Logger object

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
import shutil
#Package the files under test2 and place them in the current program directory
import shutil
import os
ret = shutil.make_archive("test2_bak", 'gztar', root_dir='test2')#The compressed package of the target can already exist
print(os.listdir())
#Package the files under test2 and place them in mkdir directory
import shutil
ret = shutil.make_archive("mkdir/test2_bak", 'gztar', root_dir='test2')
print(os.listdir("mkdir"))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['.idea', 'guessage.py', 'guessage_new.py', 'mkdir', 'requirements.txt', 'test.py', 'test2', 'test2_bak.tar.gz', '__pycache__', 'Writing file.txt', 'Writing file.txt.tmp', 'Format.py', 'Game of guessing age.jpg', 'read file.txt']
['new_ee', 'test2_bak.tar.gz']

Process finished with exit code 0
shutil The processing of the compressed package is called ZipFile and TarFile Two modules, detailed:
zipfile compress&decompression
import zipfile

# compress
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# decompression
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()

tarfile compress&decompression
import tarfile

# Compression. The compression package of egon.tar can be nonexistent, and the folder can be recursively put into the compression package
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()

# decompression
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()

Posted by sasi on Sat, 16 Nov 2019 13:11:27 -0800