Analysis on the content written in Python

Source: Internet
Author: User

This article provides a detailed description of the Python programming and IDE selection and encoding solutions. It is a must-read Python learning experience for beginners, technologies based on this language are developing rapidly, with a sharp increase in the number of users and a large number of related resources.

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 following programs are completed on Windows.

Installing Python on Windows is very convenient. You can download the binary installation package on the Windows platform for free at the official site of Python (www.python.org, the installer will complete all the configurations without setting 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. There are not many words, just a few words.

# --- 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 and uses functions in two OS modules.

  • Introduction to IronPython Compiler
  • How to correctly use Python Functions
  • Summary of Python Learning
  • Introduction to a variety of Python applications
  • Introduction to Python system files

Use OS. limitation when 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 the directory contains a file file1.rm file2.rm file3.rm, 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.

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.

If you write a statement in Python, end it with a colon, and the next line 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.

  • Introduction to IronPython Compiler
  • How to correctly use Python Functions
  • Summary of Python Learning
  • Introduction to a variety of Python applications
  • Introduction to Python system files

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. Use out = open ('names.txt ', 'w') to open a file object named out.

Open(names.txt is a new text file in the current directory. It is written in Python to indicate that the file object uses the Write Mode (Write Mode) out, And the parameters must be enclosed in single quotes. Open () is a built-in function rather than a function in the OS module, so you do not need OS. to limit the call.

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.