計算機一般來說是需要定期的清理,系統的內存不能無限延伸,同時有一些不需要的文件也可以得以清除掉。有些人會使用os.remove來進行文件的清楚,從而導致一些錯誤的出現,可以說這是對于os.remove的用法還沒有熟練掌握。下面我們就os.remove的詳細用法及使用注意分別介紹。
1.語法
2.參數
path -- 要移除的文件路徑
3.返回值
該方法沒有返回值
4.使用注意
(1)不能用來刪除文件夾,否則拒絕訪問。
# -*- coding:utf-8 -*-
import os
if __name__ == "__main__":
os.remove('D:\\test')
(2)目錄不為空會報錯
# -*- coding:utf-8 -*-
import shutil
if __name__ == "__main__":
shutil.rmtree('D:\\test')
實例擴展:
以下實例演示了 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())
執行以上程序輸出結果為:
目錄為:
[ 'a1.txt','aa.txt','resume.doc' ]
移除后 :
[ 'a1.txt','resume.doc' ]
到此這篇關于python中os.remove()用法及注意事項的文章就介紹到這了,更多相關python中os.remove()的使用注意內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- CentOS中安裝python3.8.2的詳細教程
- python 插入Null值數據到Postgresql的操作
- Python3+Django get/post請求實現教程詳解
- 安裝python依賴包psycopg2來調用postgresql的操作
- Python調用系統命令os.system()和os.popen()的實現
- Python request post上傳文件常見要點
- Python通過fnmatch模塊實現文件名匹配
- Python中fnmatch模塊的使用詳情
- python os模塊和fnmatch模塊的使用介紹