System operations

import glob
import os
import shutil
import stat
import subprocess as sub
import sys

#======================================================================+
#                                                                      |
# Methods introduced here are parts of the glob, os, sys, shutil, and  |
# subprocess modules. The main purpose of these modules are to perform |
# system information tasks, system management, and file and directory  |
# handling.                                                            |
#                                                                      |
# For further information and good introduction see the following links:
#                                                                      |
# http://docs.python.org/library/glob.html                             |
#                                                                      |
# http://docs.python.org/library/os.html                               |
# popen has been depricated; use subprocess instead                    |
#                                                                      |
# http://docs.python.org/library/shutil.html#module-shutil             |
#                                                                      |
# http://docs.python.org/library/subprocess.html                       |
#                                                                      |
# http://docs.python.org/library/sys.html                              |
#                                                                      |
#======================================================================+



#======================================================================+
#                                                                      |
# The sys module provides access to some variables used or maintained  |
# by the interpreter and to functions that interact strongly with the  |
# interpreter.                                                         |
# The most important method for me is sys.exit()                       |
#                                                                      |
#======================================================================+

sys.exit('\n   This exits the program.\n')

# This exit may be intercepted which I have not had a need for so far.

# provides a list of strings specifying the search path for modules.
sys.path()



#======================================================================+
#                                                                      |
# The os module provides a portable way of using operating system      |
# dependent functionality.                                             |
# I seem to use os.path.exists and os.system most often                |
#                                                                      |
#======================================================================+

path = os.getcwd()
print path

os.uname()

os.path.exists(path)
os.path.isdir(path)
os.path.isfile(path)

os.chdir('/home/mib')
os.getcwd()
os.chdir(path)
os.getcwd()

os.chmod(path+'/seminar4.py', 0777)
cmd = 'ls -l ' + path+'/seminar4.py'
os.system(cmd)
os.chmod(path+'/seminar4_mib.py', stat.S_IRWXU)
os.system(cmd)

newpath = 'test/nextlevel'
cmd = 'ls -l ' +newpath
os.system(cmd)
# this command works recursively and creates the complete structure
os.makedirs(newpath)
os.rmdir(newpath)
# this command does not work recursively and in this case only throws an error message
os.mkdir(newpath)
os.rmdir(newpath)


# a much more elaborate and safer way of creating a directory
# Notice: the try ... except construct is a very powerful and helpful way in python to handle exceptions
# it should actually be preferred to the if ... else construct where possible
try:
    os.makedirs(newpath)
except OSError:
    if os.path.isdir(newpath):
        pass
    else:
        # There was an error on creation, so make sure we know about it
        raise

# or using an if construct (maybe more intuitive):
if not os.path.exists(newpath):
    os.makedirs(newpath)

#======================================================================+
#                                                                      |
# Maybe the most interesting method is os.system. However, this method |
# only returns the exit status of the command to be executed. Up to    |
# python 2.5, there was the os.popen method which allowed for a bit    |
# more communication between the python script and the system command  |
# but is deprecated now.                                               |
# Since python 2.6 a much more powerful and flexible, and hence a bit  |
# more complicated, way of handling system commands is provided by the |
# subprocess module.                                                   |
#                                                                      |
#======================================================================+

process = sub.Popen('ls -l asdf', shell=True, executable='/bin/bash', stdout=sub.PIPE, stderr=sub.PIPE)
communicate = process.communicate()
if communicate[1] != '':
    sys.exit('\nAn error has occured: %s' % (communicate[1]))
else:
    listing = communicate[0].split('\n')

print listing

# Notice: the string formatting syntax %s is a very powerful and helpful tool !!!

# the correct way of getting a file listing is to use the glob method of the glob module:
# very helpful and I use it regularly in my scripts
listing = glob.glob('*.py')
print listing


#======================================================================+
#                                                                      |
# The shutil module offers a number of high-level operations on files  |
# and collections of files. In particular, functions are provided which|
# support file copying and removal. For operations on individual files,|
# see also the os module.                                              |
#                                                                      |
#======================================================================+

# !!! Always use shutil methods instead of os.system('cp src dest') !!!

os.makedirs('temp/')
shutil.copy2('seminar4.py', 'temp/seminar4.py')

# with error handling included:

# this does not work now:
os.makedirs('temp/')

# instead use this
# Nice tip: If you want to paste the following passage to ipython then you will get
# a indentation error. The right way to do it is to use the magic function %cpaste!
try:
    os.makedirs('temp/')
except:
    if os.path.isdir('temp/'): # if a dir named temp/ already exists delete it and create it anew
        try:
            os.rmdir('temp/') # does not work if dir is not empty
        except OSError:
            shutil.rmtree('temp/') # removes the complete tree
        os.makedirs('temp/')
    else: # if another error was raised let me know about it
        raise

The Python Debugger: pdb

# -*- coding: utf-8 -*-

import numpy as np
import pdb

#======================================================================+
#                                                                      |
# It is a very good idea to use a debugger when developing program     |
# source code - no matter which language is used. For python there is  |
# the python debugger pdb.                                             |
# Its documentation can be found here:                                 |
#                                                                      |
# http://docs.python.org/library/pdb.html                              |
#                                                                      |
#======================================================================+

# import pdb
# pdb.set_trace()  # where you want the program to stop and the debugger
                   # to start then use the pdb commands to debug your code

# If you run a script in ipython and uncaught exception occurs, you can use
# the ipython magic %debug to start the pdb and examine the problem.
# Alternatively, if you switch using the pdb on with calling %pdb once at
# the beginning, ipython will automatically start the debugger on any
# uncaught exception.

# With the following script the usage of the pdb is illustrated.

def main():
    ''' this is my main program'''

    # With the following script the usage of the pdb is illustrated.
    array = np.loadtxt('DE-Hai.2005.synth.hourly.allvars.csv', delimiter=',', usecols=(0,1,2,3), skiprows=1)

    a = array[:,1]
    pdb.set_trace()
    b = array[:,6]

    for i in range(len(array[:,3])):
        array[i,3] = 3.

    print 'das wärs'

if __name__ == '__main__':
    main()

LehreWiki: PythonCourse/PythonLES/SeminarLog4 (last edited 2012-08-30 07:46:01 by MichaelBorsche)