Linux-> Python script programming in Linux

Source: Internet
Author: User
Tags string methods
Linux-> Python script programming in Linux

WriteLinuxUsed
PythonScript

This article was written two years ago. The topic was locked in the script for writing Linux in Python. This article discusses the common writing method, string processing, character encoding, file and directory processing, and calling external of Python script.ProgramAnd use the built-in Link LibraryNetworkCommunication.

 

1 Linux, command draft and Python

For Linux, the script is an important part.

In the middle of the main Linux distribution, from the start of the system to the operation, can not be separated from the shell command writing. Run the following command on my HOST:

$ Ls/usr/bin/* | WC-l

2585

$ File/usr/bin/* | grep "shell script" | WC-l

267

You can find 267 shell command drafting programs, more than of all (Program) files under the/usr/bin and/usr directories. This is only part of the shell command draft.

File-orientedOperating SystemAbove, the activity of script is a matter of course. The vast majority of system settings are written in the form of strings in the configurationFileAnd the execution period information of the operating system also exists in the file system (/proc). You can directly process these strings.ManagementSystem, it is very suitable to use the command draft language for automation.

Command draft languages like python becauseDevelopmentFast relationships allow us to quickly create the system management functions we want. In addition to rapid development, python is also easy to maintain. In contrast, although the Perl program can be written shorter, it is not easy to understand. The shell command is not completely developed.Environment. Python is ideal for writing system management commandsTools.

2. Python command draft format

The basic format of the python command draft is the same as that of other languages. It is a pure text file, and # is required in the file header #! Specify the location of the literal translation program:

#! /Usr/bin/Python

Print "Hello, world! "

This is the hello. py program we wrote in the previous issue. Do not forget chmod A + x hello. py, so that you can execute this command under the command line:

$./Hello. py

Hello, world

We used to give the python program A. py extension, but the Linux Command draft does not need to be suffixed with the extension; change hello. py to hello, and the program will run normally.

The. py extension still has a special significance for python, but it is only useful when writing Python modules.

For specifying the python literal interpreter header, we generally have two methods.

It is not necessary to write an absolute path like hello. py above. We can use a relative path to specify it:

#! /Usr/bin/ENV Python

Therefore, the/usr/bin/ENV program is called a python literal interpreter to process Python program files. The advantage of this is thatInstallThere are many different Python literal interpreters that use the path at the beginning. In this way, if you have installed another version of Python (such as in your home directory) and put your python at the beginning of path setting (PATH environment variable, the Python installed by the user is used.

In addition to the python execution file, each version of Python also contains the pythonx. y file with identical content. x.y is the major version and minor version of Python. For example, Python 2.3 will have Python and python2.3, which are exactly the same in use. If the instruction draft program we write must use a certain version of Python, You can secretly put restrictions on the instruction draft header. Take Python 2.3 as an example to write the header as follows:

#! /Usr/bin/ENV python2.3

Note

Python provides an orthodox method to check information about the python and all related environments used. It is convenient to stick to the instruction draft header, but it is not the Orthodox practice of insurance; however, if the program itself is not long (such as 20 or 30 rows ), it does not have to waste time writing a series of check programs.

When the command draft only uses the standard link library of the mainstream version number (this is normal), you do not need to specify the Python version.

If the header of the absolute path in hello. py is written, the python installed in a certain position is limited.

Generally, we specify/usr/bin/Python or/usr/bin/pythonx. Y (depending on the version to be specified), that is, the python installed in the system. If it is written in this way, it is difficult for users to switch to their own installed version.

The Python literal interpreter will also read another set of headers in the format of #-*-setting-*-(usually after the first line), which are commonly used as follows:

#-*-Coding: UTF-8 -*-

The purpose is to specify the character encoding for plain text in the directive file (for UTF-8 )」. This is very important if you want to write Chinese comments. Python has a character encoding conversion mechanism, which is implemented in the codecs module, but before Python 2.4, the big5 encoding commonly used in traditional Chinese is not included in the standard codecs module. If the command file uses a character encoding that is not readable by Python (big5 and GB for the Chinese world), the python literal translator will send a warning even though the program can still be executed. If you want to write comments in Chinese, it is best to convert the command file to UTF-8 Unicode and specify the encoding above.

As mentioned in the previous issue, Python also uses # as a single-line annotation symbol (the same as shell script.). All texts after this symbol are annotated.

By the way, if you are used to editing the python command draft with vim, you can add the vim character string at the end of the file:

# VIM: Set nu et Ts = 4 Sw = 4 cino => 4:

Set the display line number (Nu) and expand the hop key (ET, for python programs, the hop key tab is the most important thing ), 4 (TS = 4), 4 (SW = 4), and 4 (cino => 4 ); then enable syntax highlighting. in vimrc ).

Using such an editing environment is very convenient for writing Python programs.

The Python literal interpreter executes the commands in the program code file in the order of appearance. If we want to write a more organized command draft, we can describe it in a tiled manner:

Print "some operations"

Change to the program code structure as follows:

Def main ():

Print "some operations"

If _ name _ = '_ main __':

Main ()

That is, you can create a "entry point" Main () function. It is more convenient when the instruction draft is long (more than one hundred lines) and will be expanded in the future.

To sum up, the common format of a python command draft should be:

#! /Usr/bin/ENV Python

#-*-Coding: UTF-8 -*-

Def main ():

Print "Hello, world"

If _ name _ = '_ main __':

Main ()

# VIM: Set nu et Ts = 4 Sw = 4 cino => 4:

3. string processing

When managing Linux systems, setting up files (in plain text) and string processing are the core part. Let's take a look at how python is doing this.

Because we have already used python to process strings and files in the previous issue, here we should give an in-depth introduction to string processing.

First, we need to know that a string is an object in Python. Open the python interactive environment (go to the shell to execute python to enter) and execute the following actions:

>>> Print type ("")

<Type 'str'>

>>> If type ("I Am a string") is str: Print true

...

True

>>> If type ("another string") is STR (): Print true

...

Type () is a built-in function of Python used to obtain the type of a variable. We can see from these three commands that the string "" and "I am a string" are all STR class objects.

View the python online files and you will find two sets of Link Libraries for string processing. One is the function in the string module, the other is a string-specific method (string methods ). Although there are some differences between the two, the features are quite repetitive; the focus of our discussion is the string method.

We often need to analyze the strings in the file: to split the strings and determine the meaning of the string data based on the given logic.

Therefore, the most common string method is the split () that we used in the previous issue ()

Split () returns a list. You can use the index value (starting with 0) to access each item in the list.

Let's demonstrate the following:

>>>Tokens = "This is a sample string used to demo split ()". Split ()

>>> Len (tokens)

9

>>> Print tokens

['Age', 'is ', 'A', 'sample', 'string', 'used', 'to', 'Demo', 'split () ']

>>> Print tokens [0], tokens [2]

This

>>> Print tokens [-1], tokens [-2]

Split () demo

>>> Print tokens [2: 5]

['A', 'sample', 'string']

The first command cut our string into nine strings, which are included in the tokens list.

Len () is a built-in function used to measure the length of an object that can store other things like a list (the number of items included in the return ).

The list can be an integer, but the maximum number of projects cannot be reached. A negative value can be input, indicating that the calculation starts from the end of the list. The index value-1 is the last item in the list.

There is a way to cut the string for judgment, we often need to output the analysis results, then we have to join the string; string format operations) this task can be completed. We can write the following representation:

>>> "% D % F % s" % (1, 1.2, "string ")

& Apos; 1 & apos; 1.200000 & apos; string & apos'

This is the string formatting operation. A formatted string with a Special conversion character (conversion character), followed by the % operator, and then a tuple as the parameter, you can fill in the data in the tuple into the format string. Commonly used % d indicates an integer with a number, % F indicates a floating point number, and % s indicates a string. For the complete conversion of the External table, see Python online files.

Note

Python tuple is also a data structure that can contain other objects. It is used to index and access the objects, but its behavior is different from that of the list. In syntax, tuple uses (1, 2, 3) for declaration, and the list uses [1, 2, 3] for declaration.If tuple has only one object, it must be written as (1,). Do not forget the comma before the right parenthesis,During string formatting, if only one character is converted, The tuple after the % operand can also be replaced by a single variable.

Another method called JOIN () for string objects can be used to combine stringsThe usage is as follows:

>>> ". Join ([" A "," B "," C "])

'Abc'

>>> "-". Join (["A", "B", "C"])

'A-B-C'

When processing strings, note that Python strings are unchangeable. That is to say, to change a character in a string, you cannot directly set it:

>>> A = "write"

>>> A [2] = "O"

Traceback (most recent call last ):

File "<stdin>", line 1, in?

Typeerror: Object doesn' t support item assignment

That is not legal. What should we do? You can do this:

>>> Print a [: 2] + "O" + A [3:]

Wrote

Although the content of a string cannot be changed, the string itself can be added (concatenated ). A [: 2] indicates the part from string a to index 2; A [3:] indicates the part from index 3 to the end of string; then access "O" in the middle ". Finally, we can get the wrote string. This operation indexTipsCan also be used on a general list.

Python also has the regular expression operation capability, which is implemented in the RE module. It is very convenient to replace the execution string.

3.1 conversion character encoding

Python has a set of codecs modules for processing character encoding. We can convert characters into different encodings freely, which is a problem that we often need to deal with when processing data in multiple languages. However, the string object itself provides the encode () and decode () methods. We can use these two methods to provide codecs capabilities without importing them into the codecs module.

Note the fact that python has two string objects. One is the STR string that we have been processing, and the other is the Unicode string that is very important for multi-language processing. In general, we use quotation marks or double quotation marks to represent normal strings (STR), while u "string" is used to represent Unicode strings. Decode () decodes common strings into Unicode objects, while encode () can encode Unicode objects into various supported character sets.

Before processing the Chinese encoding, We need to install the relevant additional kits for python 2.3: cjkcodecs and iconvcodecs; the former is a special codecs object for China, Japan, and South Korea, the latter allows python to directly use the encoding provided by the GNU iconv tool as a codecs object. Suppose we have to recode big5 as a UTF-8, then we can do this:

>>> F = open ("file. big5 ")

>>> S = f. Read ()

>>> F. Close ()

>>> Sp = S. Decode ('big5'). encode ('utf-8 ')

You canComputerFind a file whose content is big5 encoding, change locale to UTF-8, and then execute the above commands in the python interactive environment (please change the place to change ). Finally, use print S and SP to compare the strings before and after conversion.

4. File System and Directory

In Linux systems, copying, moving, and deleting files and directories are also common operations for management. The OS module provided by python can process most of the file system operations supported by the operating system. In addition, the shutil module provides higher-level operations.

4.1 file system operations

The file system is different from the file content. When operating the file system, we are dealing with moving (renaming), copying and deleting, and there is no chance to directly add new files. These actions are provided in almost all OS and shutil modules. we should import these two modules first.

ToCopy an archive, We can do this:

>>> Shutil.copy('data.txt ', 'data.new.txt ')

Delete an archiveThen OS. Unlink () is used ():

>>> OS .unlink('data.new.txt ')

There are two methods for moving (renaming):

>>> OS .rename('data.txt ', 'data.alter.txt ')

>>> Shutil.move('data.alter.txt ', 'data.txt ')

Method 1: If the source file (the first parameter) is not in the same file system as the target file (partition ), this action may fail (different UNIX methods have different processing methods ).

The second method is relatively advanced, regardless of whether the source and target files are in the same file system.

4.2 path Processing

Most management systems do not process files in the current directory, So path strings are often processed. The OS. Path module provides the handler for processing paths, which are commonly used as follows:

  • Abspath (): accepts a path string and returns the absolute path represented by this path.
  • Realpath (): accepts a path string, computes the symbolic link contained in the path, and returns the real path.
  • Split (), dirname (), basename ():

Split () accepts a path string, cut it before the last path project, and divide it into directories containing the project and the project name itself, which are returned by tuple.

Dirname () is the first element of the value returned by Split;

Basename () is the second element.

  • Join (): accepts a path list, concatenates each element in the list into a complete path string, and then returns the result.
  • Splitext (): accepts a path string, separates its extension, and returns the primary file name and extension with a tuple.
  • Exists (): test whether the input path string exists and return a Boolean value.
  • Isfile (), isdir (), islink (), isabs (): used to test whether the passed path string is a file, directory, symbolic link, or absolute path, and return a Boolean value.

In actual use, it will probably look like this:

>>> OS. Path. Split ("A/B/C ")

('A/B ', 'C ')

>>> OS. Path. Join ("A", "B", "C ")

'A/B/C'

>>> OS. Path. splitext ("DIR/file. Ext ")

('Dir/file', '. Ext ')

You can use the actual path in your directory structure to try it out!

5 external program call

Many jobs that can be completed only by calling external programs in Shell Command drafts can be completed using Python's built-in modules, such as string processing, file processing, directory processing, and so on. If you encounter Python problems or have special operations, you can also call external programs.

The OS module has a system () function that can be used to call external programs:

>>> OS. System ('LS ')

Weekly20051204.doc

Weekly20051211.doc

0

>>>

The final 0 is not the output of the LS program, but the return value.

OS. the system () function can call the simplest external program and cannot process the output data of the program. If we only want to simply execute the program, OS. the system () function will be the bestSelect.

5.1 Pipelines

When we also need to process the output data of external programs, OS. System () is not enough.

Python also has the popen2 module, which allows us to manage the output pipeline (PIPE) of external program subtrips ).

The popen2 module contains three tools: popen2 (), popen3 (), and popen4, the standard output, standard output, error output, and standard output of the sub-itinerary are redirected to the standard output and standard output respectively.

A simple example is used to describe the most commonly used popen2 () (don't forget to import popen2 first ):

>>> Stdout, stdin = popen2.popen2 ("ls ")

>>> STR = stdout. Read ()

>>> Print ostr

Weekly20051204.doc

Weekly20051211.doc

>>>

Popen2.popen2 () returns the two file objects that are linked to the LS program output. The names are stdout and stdin. After popen2.popen2 () is called, the external program will be executed immediately, and then we can read the standard output data of the external program from the stdout file object. In this way, the execution results of the program will not be directly displayed on the terminal. We can process the execution results in Python before deciding what to do.

If the program we want to call also produces an error output (stderr), and if we want to handle it, use the popen3 () or popen4 () function. The error output of popen3 () is connected to an independent archive object, while popen4 () puts the error output together in the archive object linked to the standard output; you can use it as needed.

Note

There is a new subprocess module in Python 2.4 that can execute all external program call functions. Therefore, the related functions of the OS and popen2 modules are no longer needed in Python 2.4. Of course, the old modules will not disappear, so popen2 can be used in Python 2.4, there will be no problems with our old programs.

6. Internet Communication

Python's built-in Link Library provides convenient Internet communication functions without calling external programs.

Internet communication is a large scope, with the most commonly used Global Information Network. Let's look at Zope.ApplicationProgramServiceFor example. Zope uses a zodb objectDatabaseThe system records the access actions. When a user deletes the data, the data is not actually deleted. When the user manually compresses the database, to delete the data. The action options of this compression function are placed in the Web-based zmi without the command line interface. If we do not want to manually connect to zmi for compression, you have to write a command draft that can perform HTTP operations.

The program we want to write should have the following command column interface:

Packzope. py-u <URL of Zope server>-D <day>-u <username>-P <password>

This packzope. the py program communicates with the server over HTTP and provides the user name and password obtained from the command column to the Zope server, the get method is used to tell the Zope server the number of days to be compressed (the data before the specified number of days is discarded. The following are written programs:

#! /Usr/bin/ENV Python

Import sys

Import urllib

Class parameters:

Def _ init _ (Self ):

From optparse import optionparser, optiongroup

OP = optionparser (

Usage = "Usage: % prog-u URL-D days-u username-P password ",

Version = "% prog," + "% s" % _ revision __

)

Op. add_option ("-u", Action = "Store", type = "string ",/

DeST = "url ",/

Help = "URL of site to open"

)

Op. add_option ("-d", Action = "Store", type = "int ",/

DeST = "days", default = 1 ,/

Help = "erase days before"

)

Op. add_option ("-u", Action = "Store", type = "string ",/

DeST = "username ",/

Help = "username"

)

Op. add_option ("-P", Action = "Store", type = "string ",/

DeST = "password ",/

Help = "password"

)

Self. Op = op

(Self. Options, self. ARGs) = self. Op. parse_args ()

Params = parameters ()

If not Params. Options. URL or/

Not Params. Options. username or/

Not Params. Options. Password:

Params. Op. print_help ()

SYS. Exit (1)

Url = "% S/control_panel/database/manage_pack? Days: Float = % d "%/

(Params. Options. url, Params. Options. Days)

Def main (): Try: F = myopener (). open (URL ). read () print "successfully packed zodb on host % s" % Params. options. URL destination T: Print "cannot open URL % s, aborted" % URL raiseif _ name _ = '_ main _': Main ()

The first half of the program is processed in the command line (Classparameters), AndMain ()In the form, the operation is executed.Packzope. pyExploitation
UrllibInterface to terminate the Zope server and use subclassingUrllib. fancyurlopenerThe user name and password of Alibaba Cloud. The program will output the following words after the login is completed:

Successfully packed zodb on host http: // someplace: Port

We canPackzope. pyPlace the rows in the crontab to regularly renew the rows. This is an automatic web operation.

7. Conclusion

This article describes how to use python to perform LINUX operation self-motivation, and introduces the application of Python. Of course, when any kind of Python program is developed, you can refer to the example file of Python. Dive into python is an easy-to-use free Python example. You can also find a Chinese example on the Internet.

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.