dketov
12/14/2011 - 1:01 PM

Взаимодействие с ОС

Взаимодействие с ОС

# -*- encoding: utf-8 -*-
"""
Информация о системе
"""
import platform

print 'uname:', platform.uname()

print
print 'system   :', platform.system()
print 'node     :', platform.node()
print 'release  :', platform.release()
print 'version  :', platform.version()
print 'machine  :', platform.machine()
print 'processor:', platform.processor()
# -*- encoding: utf-8 -*-
"""
Разбор опций командной строки
"""
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

(options, args) = parser.parse_args()
print options.verbose
print options.filename
# -*- encoding: utf-8 -*-
"""
Рабочий каталог и путь поиска модулей
"""
import os, sys

print 'my os.getcwd =>', os.getcwd()            
print 'my sys.path  =>', sys.path[:6]            

if 'linux' in sys.platform:
  sys.path.append(r'/home/user/python/lib')

if 'win32' in sys.platform:
  sys.path.append(r'C:\python26')
# -*- encoding: utf-8 -*-
"""
Переменные окружения
"""
import os

os.environ['USER'] = 'Mel'
os.popen('python echoenv.py').read()

import os
os.environ.keys()

os.environ['TEMP']

os.environ['TEMP'] = r'c:\temp'
os.environ['TEMP']
# -*- encoding: utf-8 -*-
"""
Завершение работы программы
"""

def bye():
    import os
    print 'Bye os world'
    os._exit(99)
    print 'Never reached'

if __name__ == '__main__': bye()
# -*- encoding: utf-8 -*-
"""
Аргументы командной строки
"""
import sys, os

print 'sys.argv[0] =', sys.argv[0]
pathname = os.path.dirname(sys.argv[0])
print 'path =', pathname
print 'full path =', os.path.abspath(pathname)

# make sure we have the right number of command line parameters
if len(sys.argv) != 3:
    print "\nSyntax: hexcal month year (where month is 1 - 12)\n" 
    sys.exit(1)

# convert the two command line parameters to numerical values
# (they come in as strings)
try:
    month = int(sys.argv[1])
    year  = int(sys.argv[2])

    # get the matrix of days and weeks in the month
    monthMatrix = monthcalendar(year, month)

except ValueError, message:
    print "Error:", message
    sys.exit(2)