Differences between revisions 5 and 6
Revision 5 as of 2008-04-15 17:05:38
Size: 1754
Editor: anonymous
Comment:
Revision 6 as of 2008-04-15 17:07:26
Size: 1797
Editor: anonymous
Comment:
Deletions are marked like this. Additions are marked like this.
Line 50: Line 50:

 
 * {{{os.path.basename()}}} Base name of pathname
Line 54: Line 53:
 

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

Modules glob

Example glob

   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)

Input/Output

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