The Python programming language solves several common practical problems

Source: Internet
Author: User

The Python programming language is a widely used computer language, but there are still many people who do not know much about the Python programming language, the following articles mainly introduce how the Python programming language solves some practical problems. The following is the main description of the article.

Python programming language solves some practical problems

The Python programming language is a concise and elegant scripting language. It has many advantages that make it easy to complete certain tasks. This article illustrates this through several specific examples. Python is a simple and elegant programming language with object-oriented features, better ability to bond other languages and cross-platform. However, I think it is equally important that it is easy to learn, and the writing code is simple and fast.

In addition, Python provides a lot of fast models, including a considerable number of functions, so as long as there is a feasible idea, it will be easier to solve it with Python. The examples below are all due to some actual problems I encountered. With Python, it is easy to solve these problems.

Automatic deletion of certain files some software will automatically generate some backup files during work. For example, when I use Vim for text editing and Autocad for drawing, these programs will automatically generate some backup files. As the number of files increases, it is necessary to clean up at intervals. Of course, you can choose to manually clean up the files, but considering that these files are scattered in different directories and there are a large number of files, manual cleaning is still a little troublesome.

I wrote a simple Python script to automatically complete this task. The following code scans all directories on disk D and deletes the backup files under the directory:

 
 
  1. from os.path import walk, join, normpath  
  2. from os import chdir, remove  
  3. def scan(arg, dirname, names)  
  4. for file in names:  
  5. 1 if file[-1:]=="~" or file[-4:]==".bak":  
  6. 2 files = normpath(join(dirname,file))  
  7. 3 chdir(dirname)  
  8. 4 print "deleting", files  
  9. 5 remove(file)  
  10. 6 print "done!"  
  11. if __name__== "__main__":   
  12. path = chdir('d:\\\\')  
  13. 7 walk(path, scan, 0)  

Brief analysis and explanation of the above Code:

The basic idea is to use a script to scan each directory and judge each file in the directory (one sentence). If it is a backup file generated by a program, it will be deleted (five sentences ). Extensions of backup files have certain characteristics. For example, the last character of the Vim backup file is the Tilde ~, The backup of Autocad ends with bak. These features are the basis for determining whether a file should be deleted.

A 7-sentence walk (path, scan, 0) is a built-in function in the Python programming language. Used to traverse the directory path. Obviously, with the help of this function provided by Python, directory scanning is easier, thus making programming much easier.

The last note is that when deleting a file, you must know its absolute path and the path must be in the directory where the file is located. Otherwise, Python will prompt that the file to be processed cannot be found. The absolute path of the file is obtained in two sentences, and the chdir (dirname) statement is used to change the current directory to the directory where the file to be deleted is located.

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.