欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python刪除文件、清空目錄的實(shí)現(xiàn)方法

 更新時(shí)間:2020年09月23日 11:50:35   作者:JohnieLi  
這篇文章主要介紹了python刪除文件、清空目錄的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Python os.remove() 方法

os.remove() 方法用于刪除指定路徑的文件。如果指定的路徑是一個(gè)目錄,將拋出OSError。

在Unix, Windows中有效

以下實(shí)例演示了 remove() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目錄
print "目錄為: %s" %os.listdir(os.getcwd())

# 移除
os.remove("aa.txt")

# 移除后列出目錄
print "移除后 : %s" %os.listdir(os.getcwd())

執(zhí)行以上程序輸出結(jié)果為:

目錄為:
[ 'a1.txt','aa.txt','resume.doc' ]
移除后 :
[ 'a1.txt','resume.doc' ]

Python os.removedirs() 方法

os.removedirs() 方法用于遞歸刪除目錄。像rmdir(), 如果子文件夾成功刪除, removedirs()才嘗試它們的父文件夾,直到拋出一個(gè)error(它基本上被忽略,因?yàn)樗话阋馕吨阄募A不為空)。

以下實(shí)例演示了 removedirs() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目錄
print "目錄為: %s" %os.listdir(os.getcwd())

# 移除
os.removedirs("/test")

# 列出移除后的目錄
print "移除后目錄為:" %os.listdir(os.getcwd())

執(zhí)行以上程序輸出結(jié)果為:

目錄為:
[  'a1.txt','resume.doc','a3.py','test' ]
移除后目錄為:
[  'a1.txt','resume.doc','a3.py' ]

Python os.rmdir() 方法

os.rmdir() 方法用于刪除指定路徑的目錄。僅當(dāng)這文件夾是空的才可以, 否則, 拋出OSError。

以下實(shí)例演示了 rmdir() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目錄
print "目錄為: %s"%os.listdir(os.getcwd())

# 刪除路徑
os.rmdir("mydir")

# 列出重命名后的目錄
print "目錄為: %s" %os.listdir(os.getcwd()) 

執(zhí)行以上程序輸出結(jié)果為:

目錄為:
[  'a1.txt','resume.doc','a3.py','mydir' ]
目錄為:
[  'a1.txt','resume.doc','a3.py' ]

Python os.unlink() 方法

os.unlink() 方法用于刪除文件,如果文件是一個(gè)目錄則返回一個(gè)錯(cuò)誤。

以下實(shí)例演示了 unlink() 方法的使用:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# 列出目錄
print "目錄為: %s" %os.listdir(os.getcwd())

os.unlink("aa.txt")

# 刪除后的目錄
print "刪除后的目錄為 : %s" %os.listdir(os.getcwd()) 

執(zhí)行以上程序輸出結(jié)果為:

目錄為:
[ 'a1.txt','aa.txt','resume.doc']
刪除后的目錄為 :
[ 'a1.txt','resume.doc' ]

其他的總結(jié)

1、remove() 同 unlink() 的功能是一樣的

在Windows系統(tǒng)中,刪除一個(gè)正在使用的文件,將拋出異常。在Unix中,目錄表中的記錄被刪除,但文件的存儲(chǔ)還在。

#使用os.unlink()和os.remove()來(lái)刪除文件
#!/user/local/bin/python2.7
# -*- coding:utf-8 -*-
import os
my_file = 'D:/text.txt'
if os.path.exists(my_file):
 #刪除文件,可使用以下兩種方法。
 os.remove(my_file)
 #os.unlink(my_file)
else:
 print 'no such file:%s'%my_file 

2、遞歸刪除目錄和文件的方法(類似DOS命令DeleteTree):

代碼如下:

import os
for root, dirs, files in os.walk(top, topdown=False):
 for name in files:
  os.remove(os.path.join(root, name))
 for name in dirs:
  os.rmdir(os.path.join(root, name)) 

3、Python清空指定文件夾下所有文件的方法: 

 這個(gè)需求很簡(jiǎn)單:需要在執(zhí)行某些代碼前清空指定的文件夾,如果直接用os.remove(),可能出現(xiàn)因文件夾中文件被占用而無(wú)法刪除,解決方法也很簡(jiǎn)單,先強(qiáng)制刪除文件夾,再重新建同名文件夾即可:

import shutil 
shutil.rmtree('要清空的文件夾名') 
os.mkdir('要清空的文件夾名') 

注:可參考這里對(duì)shutil模塊的介紹:http://www.dbjr.com.cn/article/110329.htm

如果想把一個(gè)文件從一個(gè)文件夾移動(dòng)到另一個(gè)文件夾,并同時(shí)重命名,用shutil也很簡(jiǎn)單:

shutil.move('原文件夾/原文件名','目標(biāo)文件夾/目標(biāo)文件名')

4、python 刪除非空文件夾

一般刪除文件時(shí)使用os庫(kù),然后利用os.remove(path)即可完成刪除,如果刪除空文件夾則可使用os.removedirs(path)即可,
 但是如果需要?jiǎng)h除整個(gè)文件夾,且文件夾非空時(shí)使用os.removedirs(path)就會(huì)報(bào)錯(cuò)了,此時(shí)可以使用shutil庫(kù),該庫(kù)為python內(nèi)置庫(kù),是一個(gè)對(duì)文件及文件夾高級(jí)操作的庫(kù),可以與os庫(kù)互補(bǔ)完成一些操作,如文件夾的整體復(fù)制,移動(dòng)文件夾,對(duì)文件重命名等。

import os
import shutil
os.remove(path) #刪除文件
os.removedirs(path) #刪除空文件夾
shutil.rmtree(path) #遞歸刪除文件夾

參考文獻(xiàn):

1、https://blog.csdn.net/muwinter/article/details/77196261 2018.5.25
 2、https://blog.csdn.net/qysh123/article/details/51923606 2018.5.25
 3、http://www.runoob.com/python/os-unlink.html 2018.5.25
 4、http://www.runoob.com/python/os-rmdir.html 2018.5.25
 5、http://www.runoob.com/python/os-removedirs.html 2018.5.25
 6、http://www.runoob.com/python/os-remove.html 2018.5.25

到此這篇關(guān)于python刪除文件、清空目錄的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)python刪除文件、清空目錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論