簡體   English   中英

使用python刪除文件夾中的文件

[英]Deleting files in folder with python

我有一個包含一個地理數據庫和兩個其他文件(txt)的文件夾。 我用拉鏈把它們拉鏈了。 所以現在在此文件夾中,我有gdb,txt,txt和新的zip文件。 現在,我需要刪除那些已壓縮的文件,因此該文件夾中只會是zip文件。 我寫了以下代碼:

def remove_files():
   for l in os.listdir(DestPath):
      if l.find('zipped.zip') > -1:
         pass
      else:
           print ('Deleting ' + l)
           os.remove(l)

但是得到了:

Error Info: 
[Error 2] The system cannot find the file specified: 'geogeo.gdb'

誰能幫我? 先感謝您。

os.listdir僅返回文件名,而不返回完整路徑。 如果僅給出文件名,則os.remove使用當前工作目錄。 如果當前工作目錄不同於DestPath ,那么您需要提供完整路徑:

os.remove(os.path.join(DestPath,l))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM