Python-Ref > System interaction > Running foreign programs > Simple program execution
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Simple program execution

How to run a foreign program without capturing its output of providing it standard input.
To simply run a program in the same way it would be run from the command-line (in the same environment that the script has), you can use the os.system function.
Expand/Shrink
Prostředí:
.
|-- process1-1.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (process1-1.py)
  1   import os
  2   
  3   os.system( "ls")
stdout:
process1-1.py
something
something_else
test
Doba běhu: 36.1 ms
As you can see the output of the program is simply written onto the standard output. It is of course possible to circumvent this limitation by redirecting the output of the program to a file.
Expand/Shrink
Prostředí:
.
|-- process1-2.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (process1-2.py)
  1   import os
  2   
  3   os.system( "ls > ls.txt")
Doba běhu: 36.9 ms
Expand/Shrink
Prostředí:
.
|-- process1-3.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (process1-3.py)
  1   import os
  2   
  3   os.system( "ls > ls.txt")
  4   
  5   f = file( "ls.txt", "r")
  6   for line in f:
  7       print line
  8   f.close()
stdout:
ls.txt

process1-3.py

something

something_else

test

Doba běhu: 36.6 ms
Of course you can call any program you want, not only system programs like ls.
Expand/Shrink
<examples>
  <example num="1">
    <title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="examples">
    <xsl:apply-templates select="example"/>
  </xsl:template>

  <xsl:template match="example">
    <xsl:apply-templates select="*"/>
  </xsl:template>
  
  <xsl:template match="title">
    <h1><xsl:apply-templates /></h1>
  </xsl:template>

  <xsl:template match="text">
    <p><xsl:apply-templates /></p>
  </xsl:template>
</xsl:stylesheet>
Zdroj: (process1-4.py)
  1   import os
  2   
  3   os.system( "saxon8 example.xml example.xslt > output.html")
<?xml version="1.0" encoding="UTF-8"?><h1>Example 1</h1><p>This is example nr. 1. It shows how an example looks.</p><h1>Example 2</h1><p>Another example. Imagine some ingenious text here...</p>
Doba běhu: 358.6 ms