Classic introduction to Python instance applications

Source: Internet
Author: User

As a powerful scripting language, Python brings great benefits to developers. So what is its specific application method? Today, we can use a classic Python instance application to analyze the specific application methods of this language in detail.

  • Interpretation of the basic application of Python Regular Expressions
  • Interpretation of Python compressed file basic application code example
  • Summary of common functions of the Python OS Module
  • How to find duplicate files in Python
  • Main functions of four Python files

Python has been around for 10 years and is prevalent abroad. Google search engine scripts, now popular BT (Bite Torrnet), and famous Application Server Zope are all written in Python. However, it is not widely used in China. She has her own characteristics. The syntax is concise, but it is powerful and can be used across platforms. It is well supported in Linux, Windows, and Mac. Her design is outstanding.

Here is a Python instance application that operates on disk files. Let's take a look at the characteristics of Python. The following programs are completed on Windows. It is very convenient to install Python on Windows. On the official site of Python (www.python.org), you can download the binary installation package on the Windows platform for free and install it directly. The installation program will complete all the configurations, you do not need to set environment variables like Java.

There are more than 60 video files in RM format in the folder. Now I need to extract all their file names and remove the file extension so that they can be placed on the desired webpage.

What software should be available to fulfill this simple requirement, but where can I find this software that meets the requirements? It cannot be completed manually. A powerful shell script can be used in Linux, but what about Windows users? In fact, a simple task like this can be done with only a few statements in the powerful scripting language Python. You know, you don't need to use big guys like C/C ++ or Java to complete such a task.

Let's take a look at Python's skills. You can use your favorite text editor or directly use the IDE that comes with the installation package:

 
 
  1. # --- picknames.py ---  
  2. import os  
  3. filenames=os.listdir(os.getcwd())  
  4. for name in filenames:  
  5. filenames[filenames.index(name)]=name[:-3]  
  6. out=open('names.txt','w')  
  7. for name in filenames:  
  8. out.write(name+'\n')  
  9. out.close() 


There are not many sentences for this Python instance.

# --- Picknames. py --- It is the annotation line, and Python uses # As the annotator. It must be added at the beginning of each annotation line. Python source code ends with the extension py.

Like most other languages, the import OS function library is called a module in Python.

The following sentence is a little more complex. The functions in the two OS modules are used, and the OS. limitation is used for calling. OS. getcwd () is used to return a string representing the current working directory. If the program is executed in the D: \ python directory, the function returns "D: \ python ". The program passes this string as a parameter to the OS. the listdir () function is used to return a list Of all file name strings in the directory specified by a parameter. If there is a file file1.rm file2.rm file3.rm in the directory, the return value is ['file1. rm ', 'file2. rm ', 'file3. rm ']. List one of the most common built-in data types in Python, which is represented by []. The elements are separated by commas. The element can be a variety of data types, integer, string, or a list. Python is a dynamic scripting language and can be used directly without declaring variables. Therefore, filenames = OS. listdir (OS. getcwd () assigns a list of all currently running file name strings to the variable filenames.

 
 
  1. for name in filenames:  
  2. filenames[filenames.index(name)]=name[:-3] 

The for loop in Python is different from that in other languages such as C and Java. Here, the loop uses the in keyword to assign the elements in filenames to the local variable name in sequence, so the name can get a file name string once in a loop, so that we can repeat all the file names. For is a compound statement, so end with a colon, and the next row is the loop body. I don't know whether Python contains C/C ++ or Java curly braces to identify the fast statement. This is a major feature of Python. Python uses indentation at the beginning of each line to achieve the same function.

Therefore, the space in the source code is not for the sake of appearance and easy reading, but for the syntax. This also demonstrates the concise and compact advantages of Python source code. Filenames [filenames. index (name)] = name [:-3]. In each loop, remove the name string from the name from the last three characters, that is. rm extension, and then assign it to each element in the filenames list. In Python, list and string operations are very flexible. name [:-3] returns a new string. It goes from the beginning to the last Fourth Element in name,-3 indicates the third element from the last, however, the element that does not include-3 in the extraction operation does not change the variable name itself. Filenames. index (name) returns the index of the element with the same name value in the list. In this way, the new file names can be assigned to fienames in sequence.

Then we can output them to a text file and paste them to another place. A text file with out = open Mode. w indicates that the Write Mode is used for the file object out, and the parameters must be enclosed by single quotation marks. Open () is a built-in function rather than a function in the OS module, so you do not need OS. to limit the call.

Next, it is a loop. In turn, each string in filenames is output to the out object. Pay attention to the indentation at the beginning of the line.

 
 
  1. for name in filenames:  
  2. out.write(name+'\n') 

The OUTFILE object uses writeexample (example) to write different namevalues in each cycle to the name.txt file. \ N anyone who must have learned a little programming knows that it is a line break with a conversion character. After the output is complete, call the close method to close the out object:

 
 
  1. out.close() 

This is a simple program. Save it as picknames. py and copy it to the desired directory. In the cmd command line Prompt window, type python picknames. py (the extension py is indispensable) or click run it in the folder window. The installed interpreter automatically interprets and runs the program. After running, you can see that there is another name.txt file in the folder. The content is:

 
 
  1. file1  
  2. file2  
  3. file3  
  4. ... 

The above is the introduction to the Python instance application we bring to you.

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.