Home  >  Article  >  Backend Development  >  python method to delete unnecessary python files

python method to delete unnecessary python files

不言
不言Original
2018-04-24 11:27:112183browse

The following is a python method to delete unnecessary python files. It has a good reference value and I hope it will be helpful to everyone. Let’s come and take a look

Recently I was watching Teacher Liao’s python tutorial. When I saw the operation of files, one of Teacher Liao’s paragraphs about finding python files in the computer suddenly made me want to write the python code before. I deleted it because this is the second time I watched the tutorial.

import os
for x in os.listdir('.'):
 '''
 这里的os.listdir('.')应该是定位到python所安装的目录下
 '''
 path = os.path.abspath('.')
#把目录传给path这个变量方便在删除时能够用到os.path.join(path1, path2)把完整的目录显示出来#
 if os.path.isfile(x) and os.path.splitext(x)[1] == '.py':
 #这里把后缀带.py的文件查找出来,并以列表的形式存在#
  print('准备删除'+x)
  os.remove(os.path.join(str(path), str(x)))
 #这里用os.path.join(path1, path2)把完整目录显示并删除,其中前面的path变量要用str()把path变成字符串,x量也是这样#
  print('成功删除'+x)

Related recommendations:

How to delete files and directories in python

Python deletes 2 scripts to share expired files in specified directories

The above is the detailed content of python method to delete unnecessary python files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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