怎么使用Python删除当前目录下除当前脚本以外的文件和文件夹?
末日的春天
末日的春天 2017-03-13 09:13:54
0
2
1219

怎么使用Python删除当前目录下除当前脚本以外的文件和文件夹?

末日的春天
末日的春天

Antworte allen(2)
数据分析师

怎么使用Python删除当前目录下除当前脚本以外的文件和文件夹?-PHP中文网问答-怎么使用Python删除当前目录下除当前脚本以外的文件和文件夹?-PHP中文网问答

围观一下哦,学习一下。

伊谢尔伦

用一个实例说明Python实现删除当前目录下除当前脚本以外的文件和文件夹:

import os,sys
import shutil
cur_file = os.path.basename(sys.argv[0])
dir_content = [x for x in os.listdir(".") if x != cur_file]
for f in dir_content:
  if os.path.isdir(f):
    shutil.rmtree(f)
  else:
    os.remove(f)


Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!