Python学习笔记组织文件之shutil模块

随笔记录方便自己和同路人查阅。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  shutil(或成为shell工具)模块中包含一些函数,让你在Python程序中复制、移动、改名和删除文件。要使用shutil的函数,首先需要

import shutil。

#------------------------------------------------我是可耻的分割线-------------------------------------------

  1、复制文件和文件夹

  shutil模块提供了一些函数,用于复制文件和整个文件夹。调用shutil.copy(source,destination),将路径source处的文件复制到路径

destination处的文件夹(source和destination都是字符串)。如果destination是一个文件名,它将作为被复制文件的新名字。该函数返回一个

字符串,表示被复制文件的路径。

  shutil.copy()函数,以文件夹为destination,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import shutil,os
#使用os.getcwd()函数,获取当前路径下的shutil_copy.py文件作为source,copy到D:\quiz,作为destination
shutil.copy(os.getcwd() + '\shutil_copy.py','D:\quiz')

  运行结果:

  shutil.copy()函数,以文件为destination,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import shutil,os
#使用os.getcwd()函数,获取当前路径下的shutil_copy.py文件作为source,copy到D:\quiz\shutil.txt,作为destination
shutil.copy(os.getcwd() + '\shutil_copy.py','D:\quiz\shutil.txt')

  运行结果:

  根据运行结果可以看出,以文件名为destination时,会把source内容copy到destination文件中。

   shutil.copytree()方法,拷贝整个文件夹,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import shutil,os
#使用os.chdir(),改变当前工作目录为C:UsersAdministratorPycharmProjects	est文件读写
os.chdir('C:\Users\Administrator\PycharmProjects\test\文件读写')
#shutil.copytree(),把当前目录copy到D:\quiz下,文件名称需要指定
shutil.copytree(os.getcwd(),'D:\quiz\文件读写')

  运行结果:

  在D:\quiz下创建文件读写文件夹,并把source文件夹内容copy到destination文件夹下。

  2、文件和文件夹的移动与改名

  调用shutil.move(source,destination),将路径source处的文件夹移动到路径destination,并放回新位置的绝对路径的字符串。

  如果destination指向一个文件夹,source文件将移动到destination中,并保持原来的文件名。

  shutil.move()方法,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import shutil,os
#使用shutil.move()方法,把文件移动到想要的文件夹下
shutil.move(os.getcwd() + '\shutil_move.py','d:\quiz')

  运行结果:

  根据结果可以看出,已经把shutil_move.py移动到了D盘,source文件夹下就没有了。用相同方法也可以移动文件夹。

  3、永久删除文件和文件夹

  利用 os 模块中的函数,可以删除一个文件或一个空文件夹。但利用 shutil 模块,可以删除一个文件夹及其所有的内容。

  (1)用 os.unlink(path)将删除 path 处的文件。

  os.unlink()方法,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import os
#定义一个列表,用来存储被删除的文件
filename_move = []
#打印提示信息
print('当前工作目录下文件'.center(30, '*'))
#循环
for filename in os.listdir():
    # 打印当前工作文件夹中,所有文件
    print(filename)
    #判断文件夹中,文件后缀是否为.txt
    if filename.endswith('.txt'):
        #把.txt文件追加到filename_move变量
        filename_move.append(filename)
        # 为.txt,就使用os.unlink()方法,删除该文件
        os.unlink(filename)
#打印提示信息
print('被删除文件'.center(30, '*'))
#循环打印被删除的文件
for move in filename_move:
    print(move)

  运行结果:

  (2)调用 os.rmdir(path)将删除 path 处的文件夹。该文件夹必须为空,其中没有任何文件和文件夹。

  os.rmdir()方法,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import os
print(os.rmdir('D:quiz新建文件夹'))

  运行结果:

  os.rmdir()方法,删除文件夹下存在文件,示例代码:  

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import os
print(os.rmdir('D:quizmove_test_file'))

  运行结果:

  (3)调用 shutil.rmtree(path)将删除 path 处的文件夹,它包含的所有文件和文件夹都会被删除。

  shutil.rmtree()方法,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import os,shutil
print(shutil.rmtree('D:quizmove_test_file'))

  运行结果:

  shutil.rmtree()方法,删除文件夹时,文件夹下的文件也会被删除。

  使用shutil.rmtree()方法,删除指定目录下所有文件夹,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import os,shutil
move_directory = []
# 遍历指定路径下所有文件或文件夹
for directory in os.listdir('D:quiz'):
    directory = 'D:\quiz\' + directory
    # 找指定路径所有文件夹
    if os.path.isdir(directory):
        # 使用shutil.rmtree(),删除文件夹
        shutil.rmtree(directory)
        move_directory.append(directory)
print('被删除文件夹')
for a in move_directory:
    print(a)

  运行结果:

  4、用send2trash模块安全地删除

  因为 Python 内建的 shutil.rmtree()函数不可恢复地删除文件和文件夹,所以 用起来可能有危险。删除文件和文件夹的更好方法,是使用第三方的 send2trash 模块。你可以在终端窗口中

运行 pip install send2trash,安装该模块。

  send2trash()方法,删除文件,示例代码:

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import send2trash
#send2trash.send2trash(),删除文件
send2trash.send2trash('D:\quiz\22.txt')

  send2trash()方法,删除文件夹,示例代码: 

#! python 3
# -*- coding:utf-8 -*-
# Autor: Li Rong Yang
import send2trash
#send2trash.send2trash(),删除文件
send2trash.send2trash('D:\quiz\新建文件夹')

  利用 send2trash,比 Python 常规的删除函数要安全得多,因为它会将文件夹和文件发送到计算机的垃圾箱或回收站,而不是永久删除它们。如果因程序缺陷而用send2trash 删除了某些你不想删除的东西,稍后可以从垃圾箱恢复。

一般来说,总是应该使用 send2trash.send2trash()函数来删除文件和文件夹。虽然它将文件发送到垃圾箱,让你稍后能够恢复它们,但是这不像永久删除文件,不会释放磁盘空间。如果你希望程序释放磁盘空间,就要用 os 和 shutil 来

删除文件和文件夹。请注意,send2trash()函数只能将文件送到垃圾箱,不能从中恢复文件。

原文地址:https://www.cnblogs.com/lirongyang/p/9622976.html