Python core programming version 2, 437th page, Chapter 2 exercise continued 1-answers to Python core programming-self-developed-

Source: Internet
Author: User

This is a self-made exercise and may be incorrect. You are welcome to discuss and discuss various optimization and reconstruction solutions.
Based on feedback or code review, the updated answers or related content of this article will be added to the comments of this blog.
We will try to ensure that the answer code for each question is complete, not just functions, classes, or just a rough idea. We strive to open the Python 2.7 IDLE and copy the complete code to debug and run it.
Welcome to Balian's blog home. Http://www.cnblogs.com/balian

14-5.
Commands. getoutput (). Use commands. getoutput () to solve the preceding problem.
[Answer]
Commands. getoutput () can only run on unix platforms. Reference http://bugs.python.org/issue15073
I tried to run this module in Python 2.5 of linux and found Syntax problems. Because
Deprecated since version 2.6: The commands module has been removed in Python 3. Use the subprocess module instead.

14-6.
Popen () family. Select a familiar system command that obtains text, operations, or output data from standard input. (A) Use OS. popen () to communicate with the program. Where is the output? (B) Use popen2.popen2 () instead.
[Answer]
(A) The Code is as follows:

>>> Import OS >>> command = input ('Please input a DOS command :... ') Please input a DOS command :... 'dir'> k = OS. popen (command) >>> k <open file 'dir', mode 'R' at 0x0000000002111420 >>> print k. read () # This statement can read the actual output of dir.

(B) The Code is as follows:

>>> Command = 'dir' >>> import popen2 >>> pipe_in, pipe_out = popen2.popen2 (command) >>>> pipe_in.readlines () # The result of the command dir is output here. >>> Pipe_in.close ()

[Reference]
Http://www.360doc.com/content/12/0131/16/2660674_183157204.shtml

Http://linhs.blog.51cto.com/370259/126831


14-7.

Subprocess module. Port the solution to the previous problem to the subprocess module.

[Answer]

>>> Import subprocess >>> k = subprocess. call ('dir', shell = True) # The result of the command dir is output. >>> K0 # This indicates that the command is correctly executed.

 

14-8.
Exit function. Design a function when the program exits. Install sys. exitfunc () and run the program to demonstrate that your exit function is indeed called.

[Note]

Here is an original question in the English version.

[Answer]

The Code is as follows:

import sysdef my_exit():    print 'World'    sys.exitfunc = my_exitprint 'Hello'sys.exit(1)print 'there'

[Reference]
Http://effbot.org/librarybook/sys-exitfunc-example-1.py

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.