Python 中刪除文件的幾種方法

os.remove()刪除文件os.unlink()刪除文件。它是remove()方法的Unix名稱。shutil.rmtree()刪除目錄及其下面所有內容。pathlib.Path.unlink()在Python 3.4及更高版本中用來刪除單個文件pathlib模塊。
os.remove()刪除文件os.remove()方法用于刪除文件路徑。此方法無法刪除目錄。如果指定的路徑是目錄,則該方法將引發(fā)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的文件,則上面的示例將引發(fā)錯誤。因此,最好在刪除文件之前先檢查文件是否可用。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模塊中執(zhí)行刪除功能的方法。os.unlink()和os.remove()unlink( ),而其他人則可能會使用rm命令(“刪除”的縮寫)或shell腳本來簡化語言。shutil.rmtree()刪除Python文件shutil.rmtree():刪除指定的目錄,所有子目錄和所有文件。此功能特別危險,因為它無需檢查即可刪除所有內容。結果,您可以使用此功能輕松丟失數據。rmtree()是shutil模塊下的一種方法,該方法以遞歸方式刪除目錄及其內容。Shutil.rmtree(path,ignore_errors = False,onerror = None)
path:類似路徑的對象,表示文件路徑。類路徑對象是表示路徑的字符串或字節(jié)對象。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模塊刪除文件,請創(chuàng)建一個指向該文件的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()方法適用于文件。如果指定了目錄,則會引發(fā)OSError。要刪除目錄,我們可以采用前面討論的方法之一。更多閱讀
特別推薦

點擊下方閱讀原文加入社區(qū)會員
評論
圖片
表情
