本文目錄一覽:
- 1、如何刪除和拷貝一個文件 python
- 2、Python中如何刪除一個文件
- 3、python 複製文件
- 4、python怎麼刪除文件夾以及裡面的所有文件
- 5、請教一個Python任務:複製部分文件並刪除?
- 6、如何通過Python或批處理指令刪除指定文件夾?
如何刪除和拷貝一個文件 python
copyFiles(sourceDir, targetDir):
2 if sourceDir.find(“.svn”) 0:
3 return
4 for file in os.listdir(sourceDir):
5 sourceFile = os.path.join(sourceDir, file)
6 targetFile = os.path.join(targetDir, file)
7 if os.path.isfile(sourceFile):
8 if not os.path.exists(targetDir):
9 os.make
Python中如何刪除一個文件
使用Python刪除文件有多種方法,但是最好的方法如下:
os.remove()刪除文件
os.unlink()刪除文件
shutil.rmtree()刪除目錄及其下面所有內容
pathlib.Path.unlink()在Python3.4及更高版本中用來刪除單個文件pathlib模塊。
os.remove()刪除文件
Python中的OS模塊提供了與操作系統進行交互的功能。OS屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作系統的功能的便捷式方法。
python中的os.remove()方法用於刪除文件路徑。此方法無法刪除目錄。如果指定的路徑是目錄,則該方法將引發OSError。
注意:可以使用os.rmdir()刪除目錄。
使用os.unlink()刪除Python文件
os.unlink()是os.remove()的別名。在Unix OS中,刪除也稱為unlink。
注意:所有功能和語法與os.unlink()和os.remove()相同。它們都用於刪除Python文件路徑。兩者都是Python標準庫的os模塊中執行刪除功能的方法。
它有兩個名稱,別名:os.unlink()和os.remove()。
為同一個函數提供兩個別名的可能原因是,該模塊的維護者認為,許多程序員可能會從C的底層編輯轉向Python,其中庫函數和底層系統調用稱為unlink(),而其他人則可能會使用rm命令或shell腳本來簡化語言。
使用shutil.rmtree()刪除Python文件
shutil.rmtree():刪除指定的目錄,所有子目錄和所有文件。此功能特別危險,因為它無需檢查即可刪除所有內容。結果,您可以使用此功能輕鬆丟失數據。
rmtree()是shutil模塊下的一種方法,該方法以遞歸方式刪除目錄及其內容。
使用pathlib.Path.unlink()刪除文件
pathlib模塊在Python3.4及更高版本中可用。如果要在Python2中使用此模塊,可以使用pip進行安裝。pathlib提供了一個面向對象的界面,用於處理不同操作系統的文件系統路徑。
要使用pathlib模塊刪除文件,請創建一個指向該文件的Path對象,然後對該對象調用unlink()方法。
python 複製文件
用Python把某一目錄下的文件複製到指定目錄中,代碼如下:
1、首先插入必要的庫:
import os
import os.path
import shutil
import time, datetime
2、實現複製文件代碼如下:
def copyFiles(sourceDir,targetDir):
if sourceDir.find(“.svn”) 0:
return
for file in os.listdir(sourceDir):
sourceFile = os.path.join(sourceDir,file)
targetFile = os.path.join(targetDir,file)
if os.path.isfile(sourceFile):
if not os.path.exists(targetDir):
os.makedirs(targetDir)
if not os.path.exists(targetFile) or(os.path.exists(targetFile) and (os.path.getsize(targetFile) != os.path.getsize(sourceFile))):
open(targetFile,”wb”).write(open(sourceFile,”rb”).read())
if os.path.isdir(sourceFile):
First_Directory = False
copyFiles(sourceFile, targetFile)
3、主函數的實現:
if __name__ ==”__main__”:
print “Start(S) or Quilt(Q) \n”
flag = True
while (flag):
answer = raw_input()
if ‘Q’ == answer:
flag = False
elif ‘S’== answer :
formatTime = getCurTime()
targetFoldername = “Build ” + formatTime + “-01”
Target_File_Path += targetFoldername
copyFiles(Debug_File_Path,Target_File_Path)
removeFileInFirstDir(Target_File_Path)
coverFiles(Release_File_Path,Target_File_Path)
moveFileto(Firebird_File_Path,Target_File_Path)
moveFileto(AssistantGui_File_Path,Target_File_Path)
writeVersionInfo(Target_File_Path+”\\ReadMe.txt”)
print “all sucess”
else:
print “not the correct command”
python怎麼刪除文件夾以及裡面的所有文件
os包的rmdir()函數可以用來刪除一個文件夾,但是文件夾必須是空的。一種可行的方法是讀取文件夾的文件列表,逐個刪除文件夾中的所有文件,然而文件夾中可能還有文件夾,因此這是一個遞歸的操作。
shutil包rmtree()函數就實現了以上功能。shutil是一個高級文件操作的包,實現了文件及文件集合複製與刪除的功能。rmtree()函數接收非空文件夾的路徑這唯一一個參數。示例代碼如下:
import
shutil
path
=
‘g:\zhidao’
shutil.rmtree(path)
請教一個Python任務:複製部分文件並刪除?
隨機抽取十個文件可以獲取每個文件夾的文件,生成一個列表(os.listdir),獲取文件夾同理。
移動可以藉助os.rename()
格式如下:
os.rename(src, dst)
src – 要修改的目錄名
dst – 修改後的目錄名
用絕對路徑把文件名稱前面的路徑改成
…\new_folder\
(…自己填)
就完成了剪切操作
如何通過Python或批處理指令刪除指定文件夾?
不清楚你的實際文件/情況,僅以問題中的樣例說明及猜測為據;以下代碼複製粘貼到記事本,另存為xx.bat,編碼選ANSI,跟要處理的文件放一起雙擊運行@echo offcd /d “%~dp0″mode con lines=5000
rem 根據一個txt文本文件里列出的路徑,將指定文件或文件夾刪除
set #=Any questionset _=WXset $=Qset/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
set “txtfile=xxx.txt”
if not exist “%txtfile%” (echo;”%txtfile%” not foundpauseexit)
for /f “delims=” %%a in (‘type “%txtfile%”‘) do (
set “isexist=”
echo;”%%~a”
if exist “%%~a\” (
set “isexist=1″rd /s /q “%%~a\”
)
if exist “%%~a” (
set “isexist=1″del /a /f /q “%%~a”
)
if not defined isexist (echo;not found)
)
echo;%#% +%$%%$%/%_% %z%
pause
exit
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/183919.html