Python 中删除文件的几种方法

os.remove()删除文件os.unlink()删除文件。它是remove()方法的Unix名称。shutil.rmtree()删除目录及其下面所有内容。pathlib.Path.unlink()在Python 3.4及更高版本中用来删除单个文件pathlib模块。
os.remove()删除文件os.remove()方法用于删除文件路径。此方法无法删除目录。如果指定的路径是目录,则该方法将引发OSError。os.rmdir()删除目录。remove()方法删除Python文件的语法:os.remove(path)
path—— 这是要删除的路径或文件名。
remove()方法没有返回值。os.remove函数删除Python文件的示例。OS.Remove()方法删除文件的基本示例。# Importing the os library
import os
# Inbuilt function to remove files
os.remove("test_file.txt")
print("File removed successfully")
File removed successfully
testfile.txt的文件的路径。解释程序流程的步骤如下:remove()方法。os.remove()删除文件的路径。“ test_file.txt”。您可以在此处放置所需的文件。test_file.txt的文件,则上面的示例将引发错误。因此,最好在删除文件之前先检查文件是否可用。Os.Path.Isfile检查文件是否存在并使用Os.Remove删除它os.remove()方法将在工作目录中搜索要删除的文件。因此,最好检查文件是否存在。os.path.isfile来检查文件的可用性。#importing the os Library
import os
#checking if file exist or not
if(os.path.isfile("test.txt")):
#os.remove() function to remove the file
os.remove("demo.txt")
#Printing the confirmation message of deletion
print("File Deleted successfully")
else:
print("File does not exist")
#Showing the message instead of throwig an error
File Deleted successfully
os.pasth.isfile()方法。这种方法有助于我们找出文件是否存在于特定位置。import os
from os import listdir
my_path = 'C:\Python Pool\Test\'
for file_name in listdir(my_path):
if file_name.endswith('.txt'):
os.remove(my_path + file_name)
.txt的所有文件。从os模块导入os模块和 listdir。必须使用listdir才能获取特定文件夹中所有文件的列表,并且需要os模块才能删除文件。my_path是包含所有文件的文件夹的路径。我们正在遍历给定文件夹中的文件。 listdir用于获取特定文件夹中所有文件的一个列表。endswith用于检查文件是否以.txt扩展名结尾。当我们删除文件夹中的所有.txt文件时,如果条件可以验证,则进行此操作。如果文件名以 .txt扩展名结尾,我们将使用os.remove()函数删除该文件。此函数将文件的路径作为参数。my_path+file_name是我们要删除的文件的完整路径。
*符号作为模式字符串。#Importing os and glob modules
import os, glob
#Loop Through the folder projects all files and deleting them one by one
for file in glob.glob("pythonpool/*"):
os.remove(file)
print("Deleted " + str(file))
Deleted pythonpool\test1.txt
Deleted pythonpool\test2.txt
Deleted pythonpool\test3.txt
Deleted pythonpool\test4.txt
pythonpool文件夹中的所有文件。glob.glob()方法将获取所有文件夹内容的名称,无论它们是文件还是子文件夹。因此,请尝试使模式更具体(例如*.*),以仅获取具有扩展名的内容。os.unlink()删除Python文件os.unlink()是os.remove()的别名。在Unix OS中,删除也称为unlink。os.unlink()和os.remove()相同。它们都用于删除Python文件路径。两者都是Python标准库的os模块中执行删除功能的方法。os.unlink()和os.remove()unlink( ),而其他人则可能会使用rm命令(“删除”的缩写)或shell脚本来简化语言。shutil.rmtree()删除Python文件shutil.rmtree():删除指定的目录,所有子目录和所有文件。此功能特别危险,因为它无需检查即可删除所有内容。结果,您可以使用此功能轻松丢失数据。rmtree()是shutil模块下的一种方法,该方法以递归方式删除目录及其内容。Shutil.rmtree(path,ignore_errors = False,onerror = None)
path:类似路径的对象,表示文件路径。类路径对象是表示路径的字符串或字节对象。ignore_errors:如果ignore_errors为true,则删除失败导致的错误将被忽略。oneerror:如果ignore_errors为false或被忽略,则通过调用onerror指定的处理程序来处理此类错误。Shutil.Rmtree()删除文件的Python程序# Python program to demonstrate shutil.rmtree()
import shutil
import os
# location
location = "E:/Projects/PythonPool/"
# directory
dir = "Test"
# path
path = os.path.join(location, dir)
# removing directory
shutil.rmtree(path)
pathlib.Path.unlink()删除文件pathlib模块在Python 3.4及更高版本中可用。如果要在Python 2中使用此模块,可以使用pip进行安装。pathlib提供了一个面向对象的界面,用于处理不同操作系统的文件系统路径。pathlib模块删除文件,请创建一个指向该文件的Path对象,然后对该对象调用unlink()方法:Pathlib删除文件的Python程序
#Example of file deletion by pathlib
import pathlib
rem_file = pathlib.Path("pythonpool/testfile.txt")
rem_file.unlink()
path()方法用于检索文件路径,而unlink()方法用于删除指定路径的文件。unlink()方法适用于文件。如果指定了目录,则会引发OSError。要删除目录,我们可以采用前面讨论的方法之一。更多阅读
特别推荐

点击下方阅读原文加入社区会员
评论
