Below for you to share a python delete the unwanted Python file method, has a good reference value, I hope to help you. Come and see it together.
In the recent Python tutorial to see Teacher Liao, when I saw the operation of the file, Mr. Liao's part of the search for a python file on the computer, suddenly want to write the Python code to delete, because this is the second time to see the tutorial.
Import Osfor x in Os.listdir ('. '): "Here's Os.listdir ('. ') Should be located in the directory where Python is installed "path = Os.path.abspath ('. ') #把目录传给path这个变量方便在删除时能够用到os. Path.join (path1, path2) displays the complete directory as if Os.path.isfile (x) and Os.path.splitext (x) [1] = = '. Py ': #这里把后缀带. py file is found and exists as a list of # print (' Ready to delete ' +x) os.remove (Os.path.join (str (path), str (x))) # Here with Os.path.join (path1, path2) The full directory is displayed and deleted, where the previous path variable to use STR () to turn path into a string, x amount is the same # print (' successful delete ' +x)