Operating shutil library files

A, copy, move, rename

import statement the shutil

old_file = r " C: \ the Users \ ffm11 \ Desktop \ AI.docx " 
new_file = r " C: \ the Users \ ffm11 \ Desktop \ AI_new.docx " 
# copy files and permissions 
shutil.copy (old_file, new_file)

# Change the owner and group of files, only in linux 
shutil.chown (old_file, User = " Maple " , Group = " Python " )

# Copy the file and copy all the statistical information, such as modification time. 
shutil.copy2 (old_file, new_file)

# Recursively to copy the folder 
src = r " C: \ the Users \ ffm11 \ Desktop \ machine learning " 
dst = r " C: \ the Users \ ffm11 \ Desktop \ machine learning 1 "
shutil.copytree (src, dst)

# Recursively to delete files 
shutil.rmtree (dst)

# Recursively to move a file, which is similar mv command, in fact, renamed. 
shutil.move (src, dst)

Second, the compression and decompression of files

import statement the shutil

old_file=r"C:\Users\ffm11\Desktop\AI.docx"
new_file=r"C:\Users\ffm11\Desktop\AI_new.docx"

# Compresses all files in the directory to the src dst a directory named machine learning, compression format is the tar 
shutil.make_archive (dst + " / machine learning 1 " , ' the tar ' , ROOT_DIR = src)


# Extract the files to the file 
filename = r " C: \ the Users \ \ Desktop \ machine learning ffm11 1 \ machine learning 1.tar " 
extract_dir = r " C: \ the Users \ ffm11 \ Desktop \ machine learning 2 " 
shutil.unpack_archive ( filename, extract_dir)

Guess you like

Origin www.cnblogs.com/angelyan/p/12041075.html