Python removes unwanted Python file methods

Source: Internet
Author: User
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.