自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python代碼來斷定某個(gè)文件夾內(nèi)是否存在相關(guān)文件的方案

開發(fā) 后端
你如果用Python代碼來斷定某個(gè)文件夾內(nèi)是否存在相關(guān)的文件時(shí),或者是否已被刪除或已被移走時(shí),以下的文章就是對(duì)這一Python代碼的相關(guān)方案的介紹。

Python是計(jì)算機(jī)語言中常用的語言,在實(shí)際的應(yīng)用中你好似接觸到Python代碼的實(shí)際應(yīng)用?你對(duì)其是否又所了解?以及是否用其解決過類似于判斷這在文件夾內(nèi)是否存在相關(guān)的文件的的實(shí)際應(yīng)用?如果你有同樣的問題需要解答的話,你就可以點(diǎn)擊以下的文章了。

提問者: yangzedong66 - 二級(jí)***答案按照你的問題寫的

  1. #encoding=utf-8  
  2. import os 

輸入要查看的目錄名,如“e:\ArcGis”

  1. inputdir = raw_input("input:")  
  2. print os.listdir(inputdir) 

 

輸入要?jiǎng)h除的文件或者目錄名

  1. inputtodelete = raw_input("file to delete:") 

如果是目錄,Python代碼的相關(guān)實(shí)際應(yīng)用中則進(jìn)行循環(huán)刪除

  1. if os.path.isdir(inputtodelete):  
  2. for root, dirs, files in os.walk(inputdir + os.sep + inputtodelete,topdown=False):   
  3. for file in files:  
  4. os.remove(os.path.join(root,file))  
  5. for dir in dirs:  
  6. os.rmdir(os.path.join(root,dir))  
  7. os.rmdir(inputdir + os.sep + inputtodelete)  
  8. print "dir deleted!" 

是文件,則直接刪除

  1. elif os.path.isfile(inputtodelete):  
  2. os.remove(inputdir+os.sep+inputtodelete)  
  3. print "file deleted!"  
  4. else:  
  5. print "error!not file or dir"  

以上的相關(guān)內(nèi)容就是對(duì)Python代碼解決相關(guān)問題的具體介紹。

【編輯推薦】

  1. 對(duì)Python源代碼組織的相關(guān)實(shí)際操作步驟解析
  2. Python安裝的步驟操作其實(shí)是件很容易的事
  3. Python源碼分析的實(shí)際相關(guān)操作步驟解析
  4. Python學(xué)習(xí)入門中的先搭環(huán)境的具體應(yīng)用的方法介紹
  5. Python中文轉(zhuǎn)換url編碼的實(shí)際操作步驟介紹
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2010-03-19 15:16:11

Python代碼

2023-03-28 15:19:37

文件列表scandir函數(shù)

2020-09-23 08:53:48

父文件夾模塊Python

2024-09-25 10:51:21

Python文件夾管理

2010-03-17 14:42:09

Python 文件

2024-12-06 15:11:34

Python文件夾目錄

2010-01-13 10:25:30

VB.NET文件夾操作

2009-09-02 19:22:03

C#遞歸

2021-11-17 09:01:23

Python批量合并Python基礎(chǔ)

2011-08-04 15:36:32

文件夾病毒

2009-12-03 10:18:32

Linux文件夾執(zhí)行權(quán)限

2017-10-16 11:13:36

微信小程序保存位置

2010-03-24 09:29:30

Python os.l

2010-12-31 13:35:25

文件夾重定向

2021-11-19 08:59:28

Python 批量合并

2010-03-15 12:50:19

Python文件夾創(chuàng)建

2009-08-12 16:57:28

C#讀取文件夾

2011-03-04 16:37:13

FileZilla

2009-12-01 09:13:51

shell腳本linux

2015-03-13 13:50:47

Java讀取文件夾大小Java讀取文件Java讀取
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)