In python, the command line is called.

Source: Internet
Author: User

In python, the command line is called.

Http://blog.csdn.net/pipisorry/article/details/46972171

In the Python/wxPython environment, execute External commands or start another program in the Python program.

1. OS. system (command)

The OS. system () function is used to run shell commands. This command can be used to conveniently call or execute other scripts and commands.

  • # Open a specified file
  • >>> OS. system ('notepad *. txt ')

  • 2. wx. Execute (command, syn = wx. EXEC_ASYNC, callback = None)

    If the syn is set to wx. EXEC_ASYNC, the wx. Excute function returns immediately. If the syn is set to wx. EXEC_SYNC, the system returns the result after the called program ends.

    Callback is a wx. Process variable. If callback is not set to None and syn = wx. EXEC_ASYNC, the wx. Process. OnTerminate () function is called after the program ends.

    Both OS. system () and wx. Execute () use the system shell. A shell window appears during execution. For example, the console window appears in Windows, which is not beautiful. The following two methods do not have this disadvantage.

    3. import subprocess

    Subprocess. popen (args, bufsize = 0, executable = None, stdin = None, stdout = None, stderr = None, preexec_fn = None, close_fds = False, shell = False, cwd = None, env = None, universal_newlines = False, startupinfo = None, creationflags = 0)

    subprocess.call (["cmd""arg1""arg2"],shell=True)

    Note:

    1. The args parameter can be a string or sequence type (such as list and tuples), used to specify the executable file of the process and its parameters.

    2. If command (args) is not an executable file, shell = True cannot be saved

    3. Popen. wait () can get the return value of the command.

    Example

    if subprocess.Popen('dir /b %s' % filename, shell=True).wait() == 0
    If the filename string contains spaces, the following error occurs: File Not Found.

    You can use the following["cmd""arg1""arg2"]Two methods

    if subprocess.Popen(["dir", "/b", filename], shell=True).wait() == 0

    if subprocess.call(["dir", "/b", filename], shell=True) == 0

    [Subprocess learning in Python]

    4. webbrowser. open (url)

    The preceding three methods can only be used to execute programs and open files, but cannot process URLs. You can use the functions provided by the webbrowser module to open URLs.

    You can call the system's default browser to open a URL, such as webbrowser. open ('HTTP: // www.jb51.net ').
    Webbrowser. open ('H: \ python.zip ') to run the program. In this way, you do not need to identify the file name or URL, and do not know whether it is feasible in Linux.
    The above code is run in Windows2000, Python2.4a1, and wxPython 2.5.1.
    [Python method for calling shell]

    From: http://blog.csdn.net/pipisorry/article/details/46972171

    Ref: Execute linux commands in python (call linux commands)


    Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

    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.