Delete files with python, non-empty directory scripts

  
                  

A small script written in python, delete non-empty directories and files, the code is as follows: #!/usr/bin/python #encoding=utf8 import os,sys if len(sys.argv) > 1: for filename In sys.argv[1:]: if os.path.isdir(filename): for root, dirs, files in os.walk(filename,topdown=False): for name in files: os.remove(os.path. Join(root, name)) print os.path.join(root,name) for name in dirs: os.rmdir(os.path.join(root, name)) print "delete %s" % (os.path .join(root,name)) os.rmdir(filename) else: os.remove(filename) else: print "Using method: rm.py filename1 filename2....." The principle is very simple with os.walk function Traversing the directory, topdown=False is this Given traverse from bottom to top, is not provided or if provided topdown = True, it is traversed from top to bottom.

Originally, I wanted to implement a function that traverses the directory. As a result, Python has already been provided, which is much more convenient

Copyright © Windows knowledge All Rights Reserved