Differences between revisions 1 and 8 (spanning 7 versions)
Revision 1 as of 2008-04-14 10:59:03
Size: 206
Editor: anonymous
Comment:
Revision 8 as of 2008-04-16 20:52:40
Size: 2426
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
#acl AdminGroup:read,write EditorGroup:read All:read #acl AdminGroup:read,write
Line 5: Line 5:
SiaProgrammingPython
Line 6: Line 7:
= I/O =
<<TableOfContents(2)>>
Line 10: Line 13:
The modules sys and os provide the basic interface to the operating system.
The module os creates a portable abstraction layer which is used by high-level modules like glob, socket, thred, time, fcntl.

Line 11: Line 18:
The module [[http://docs.python.org/lib/module-sys.html|sys]] provides access to system-specific parameters
by the interpreter.

=== Example argv ===
{{{#!python
#system1.py
import sys
print sys.argv
}}}

{{{
run system1.py parameter1 parameter2
['system1.py', 'parameter1', 'parameter2']
}}}
The script prints the command line arguments that are passed to the script. argv[0] is the script name

A more sophisticated way of evaluating command line arguments is provided by the module
[[http://docs.python.org/lib/module-optparse.html|optparse]]
Line 13: Line 38:
The module [[http://docs.python.org/lib/module-os.html|os]] is a portable
operating system interface.

Some examples:

 * {{{os.system()}}} Executes the command (a string) in a subshell
 * {{{os.mkdir()}}} Creates a directory
 * {{{os.remove()}}} Deletes a file
 * {{{os.path.isdir()}}} Test if directory
 * {{{os.path.isfile()}}} Test if file
 * {{{os.path.exists()}}} Test if file or directory exists
 * {{{os.path.getsize()}}} Size of a file
 * {{{os.path.basename()}}} Base name of pathname
 


== Module glob ==

The module [[http://docs.python.org/lib/module-glob.html|glob]] finds all the pathnames matching a specified pattern according to the rules used by the Unix shell.

The following example looks for all pdf files in the current working directory and converts them into postscript files.
{{{#!python
#!/usr/bin/env python
import os,glob

filelist=glob.glob('*.pdf')
for f in filelist:
    psfilename=f.split('.')[0]+'.ps'
    cmd='pdftops '+f+' '+psfilename
    print cmd
    os.system(cmd)
}}}
= Unix Specific Services =
Features that are unique to the Unix operating system are for example shell pipelines that ''pipe'' the output of one program to another. The pipeline symbol is {{{|}}}. For example, the command {{{ls -s | sort -rg}}} ''pipes'' the output of {{{ls}}} whit the option s (size) to the {{{sort}}} program. The result is a list of filenames sorted by its size

SiaProgrammingPython

Interaction with the operating system

The modules sys and os provide the basic interface to the operating system. The module os creates a portable abstraction layer which is used by high-level modules like glob, socket, thred, time, fcntl.

Module sys

The module sys provides access to system-specific parameters by the interpreter.

Example argv

   1 #system1.py
   2 import sys
   3 print sys.argv

run system1.py parameter1 parameter2
['system1.py', 'parameter1', 'parameter2']

The script prints the command line arguments that are passed to the script. argv[0] is the script name

A more sophisticated way of evaluating command line arguments is provided by the module optparse

Module os

The module os is a portable operating system interface.

Some examples:

  • os.system() Executes the command (a string) in a subshell

  • os.mkdir() Creates a directory

  • os.remove() Deletes a file

  • os.path.isdir() Test if directory

  • os.path.isfile() Test if file

  • os.path.exists() Test if file or directory exists

  • os.path.getsize() Size of a file

  • os.path.basename() Base name of pathname

Module glob

The module glob finds all the pathnames matching a specified pattern according to the rules used by the Unix shell.

The following example looks for all pdf files in the current working directory and converts them into postscript files.

   1 #!/usr/bin/env python
   2 import os,glob
   3 
   4 filelist=glob.glob('*.pdf')
   5 for f in filelist:
   6     psfilename=f.split('.')[0]+'.ps'
   7     cmd='pdftops '+f+' '+psfilename
   8     print cmd
   9     os.system(cmd)

Unix Specific Services

Features that are unique to the Unix operating system are for example shell pipelines that pipe the output of one program to another. The pipeline symbol is |. For example, the command ls -s | sort -rg pipes the output of ls whit the option s (size) to the sort program. The result is a list of filenames sorted by its size

LehreWiki: SiaProgrammingPythonSystem (last edited 2008-04-21 12:09:59 by anonymous)