Python instance application

Source: Internet
Author: User

She is an open source scripting language originally developed by Guido van rosum.

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 are two examples of using python, which are operations on disk files. Let's take a look at the characteristics of Python. The followingProgramIt is completed on the Windows platform. 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.

Scenario 1:
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:
# --- Picknames. py ---
Import OS
Filenames = OS. listdir (OS. getcwd ())
For name in filenames:
Filenames [filenames. Index (name)] = Name [:-3]
Outsourcopen('names.txt ', 'w ')
For name in filenames:
Out. Write (name + '\ n ')
Out. Close ()

There are not many words in a sentence.
# --- Picknames. py --- It is the annotation line, and python uses # As the annotator. It must be added at the beginning of each annotation line. PythonSource codeIs the end of a py extension.

like most other languages, import the OS function library, which is called a module in Python terminology.
the next 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.

For name in filenames:
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 sourceCodeThe space in 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.
For name in filenames:
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: 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:
File1
File2
File3
...

The first task is easily completed. Let's take a look at the next one.

Scenario 2:
I have some image files. I want to change their file names to: 00.bmp 01.bmp 02.bmp... 23.bmp 24.bmp...

It seems that such a task could not be completed by any software at once. It should be written in Python. This time, I wrote the comments directly in the source code:
# --- Changeall. py ---
# Importing the OS Module
Import OS, sys

# Retrieve the list of file names in the current working directory.
Filenames = OS. listdir (OS. getcwd ())

Add 0 to the file name before 1.bmp to 9.bmp.
# Len () returns the length of the filenames list, And xrange () returns a series of length N from 0 to the filenames list for loop.
For num in xrange (LEN (filenames )):

# Exclude the program file by using the if judgment statement
# Use the rename (oldname, newname) function in the OS module to change the file name.
The top 10 files with names ranging from 0 to 9 are 0, that is, 00.bmp 01.bmp...
# STR () function returns the string of the numeric type variable
# Note the indentation at the beginning of the line in the second if statement
If filenames [num]! = 'Changeall. py ':
If num <10:
OS .rename(filenames{num},'0'{str(num}}'.bmp ')
Else:
OS .rename(filenames{num},str(num={'.bmp ')

now, after reading the programming in two scenarios, you can also have a rough understanding of Python. I hope you will like the language of this open-source project with elegant syntax and powerful functions. It can efficiently complete many tasks. More information can be found at www.python.org. There are not many Chinese documents on the Internet.

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.