Detailed example of using python to filter and delete files in a directory

小云云
Release: 2017-12-27 14:47:14
Original
2263 people have browsed it

This article mainly introduces you to the relevant information on how to use Python to filter and delete files in a directory. The article introduces it in great detail through sample code. It has certain reference learning value for everyone's study or work. Friends who need it Let’s learn together with the editor below. Hope it helps everyone.

Preface

I recently learned python and feel that it can be used in many places. Packaging, testing, uploading, and movie crawling....and the amount of code is really small. Life is short, I use python. The reason I write this today is because when downloading a movie, you will always find these two files in addition to the video, and even more messy files


## It's uncomfortable looking at these documents. Deleting folders one by one is too laborious. Also be careful not to delete by mistake. So this script came out to "filter and delete files based on extensions". I won't say much below, let's take a look at the detailed introduction.

Step1

First you need a parameter configuration file FilterParameter.py

  • formatFiles extension that needs to be filtered

  • dir Directory path that needs to be deleted


formatFiles = [
 '.mp4',
 '.mkv',
 '.avi',
 '.rmvb'

]
dir = "/Users/cuiyang/Movies/Fmovie/"
Copy after login
Step2

First create a method to remove all files in the directory

def currentDirFile(dir):
 fileNames = os.listdir(dir)
 for fn in fileNames:
  fullFileName = os.path.join(dir, fn)
  if not os.path.isdir(fullFileName):
   delFile(fullFileName)
  else:
   currentDirFile(fullFileName)
Copy after login
Then Filter the files that need to be deleted. Here, move the files to the trash (mac). If the directory is written incorrectly or deleted accidentally for some reason, then there is no need to do it.

def delFile(filePath):
 # 分隔后缀名
 formatName = os.path.splitext(filePath)[1]
 if not FilterParameter.formatFiles.__contains__(formatName) and \
     filePath.split('/')[-1] != '.DS_Store': # mac下每个文件夹都有个.DS_Store隐藏文件这个不需要动
  # print(filePath)
  shutil.move(filePath, '/Users/cuiyang/.Trash')# 移动到废纸篓
Copy after login
Yes, it’s that simple. I believe students who know Python will understand it immediately.

Related recommendations:

Detailed explanation of python regular expression re.sub & re.subn

Explanation of python user management system

How to draw a line chart using python

The above is the detailed content of Detailed example of using python to filter and delete files in a directory. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!