Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

PythonÖеÄOSÄ£¿é

osÄ£¿éÌṩÁ˶àÊý²Ù×÷ϵͳµÄ¹¦Äܽӿں¯Êý.µ±osÄ£¿é±»µ¼Èëºó,Ëü»á×ÔÊÊÓ¦ÓÚ²»Í¬µÄ²Ù×÷ϵͳƽ̨,Èçposix»òNTϵͳƽ̨,osÄ£¿é»á¸ù¾Ý²»Í¬µÄƽ̨½øÐÐÏàÓ¦µÄ²Ù×÷.±¾½ÚÄÚÈݽ«¶ÔosÄ£¿éÌṩµÄº¯Êý½øÐÐÏêϸµÄ½â¶Á.
1.1 Îļþ²Ù×÷º¯Êý
1.1.1 open()º¯ÊýÌṩ´´½¨¡¢´ò¿ª¡¢ÐÞ¸ÄÎļþµÄ¹¦ÄÜ¡£
Example 1-1. Using the os Module to Rename and Remove Files
#Filename: os-example-1.py
import os
import string
def replace(file, search_for, replace_with):
    # replace strings in a text file
    back = os.path.splitext(file)[0] + ".bak"
    temp = os.path.splitext(file)[0] + ".tmp"
    try:
        # remove old temp file, if any
        os.remove(temp)
    except os.error:
        pass
    fi = open(file)
    fo = open(temp, "w")
    for s in fi.readlines():
        fo.write(string.replace(s, search_for, replace_with))
    fi.close()
    fo.close()
    try:
        # remove old backup file, if any
        os.remove(back)
    except os.error:
        pass
    # rename original to backup...
    os.rename(file, back)
    # ...and temporary to original
    os.rename(temp, file)
# try it out!
file = "samples/sample.txt"
replace(file, "hello", "tjena")
replace(file, "tjena", "hello")
1.1.2 rename()ºÍremove()º¯Êý,¶ÔÎļþ½øÐÐÖØÃüÃûºÍɾ³ý²Ù×÷.Çë²ÎÕÕÀý1-1
1.2 Ŀ¼²Ù×÷
1.2.1 listdir()º¯Êý,·µ»ØÖ¸¶¨Ä¿Â¼ÏÂËùÓÐÎļþÃû,²¢±£´æÓÚÒ»ÁбíÖÐ.µ«µ±Ç°Ä¿Â¼±ê¼Ç(.)ºÍ¸¸Ä¿Â¼±ê¼Ç(..)²»ÔÚÆäÖÐ.
Example 1-2. Using the os Module to List the Files in a Directory
File: os-example-2.py
import os
for file in os.listdir("samples"):
 


Ïà¹ØÎĵµ£º

ÓÃPythonÖ±½ÓдUTF 8Îı¾Îļþ

µ±ÎÒÃÇÕâÑù½¨Á¢Îļþʱ
f =
file('x1.txt', 'w')
f.write(u'ÖÐÎÄ')
f.colse()
Ö±
½Ó½á¹ûÓ¦¸ÃÊÇÀàËÆ
    f.write(u'ÖÐÎÄ')
UnicodeEncodeError: 'ascii'
codec can't encode characters in position 0-16: ordinal not in
range(128)
ÒªÖ±½Óд utf-8 ÎļþÔõô°ìÄØ?
import codecs
f = codecs. ......

ÈçºÎÔÚApacheÏÂÉèÖÃPython(WIN,CGI·½·¨)

¹ØÓÚPython£¬±¾È˲»Ïë¶à˵ÁË£¬Èç¹û²»ÖªµÀʲôÊÇPython£¬¿ÉÒÔ¿´¡¶Ê²Ã´ÊÇPython
£¿¡·Ò»ÎÄ¡£Python¿ÉÒÔ¿ª·¢CGI³ÌÐò£¬ÄÇôÔÚApacheÏÂÓ¦ÈçºÎÅäÖÃÄØ£¿±¾ÎÄÖ»½²ÊöÁËÈçºÎ
ÒÔCGI·½Ê½ÅäÖÃApache£¬Ê¹ÆäÖ§³ÖPython³ÌÐò¡£ÆäËü·½Ê½£¬Èçmod_pythonÔò²»ÔÚÌÖÂÛ·¶Î§
(ÆäʵÊÇûÅä³É¹¦:-)¡£ÎÒËùʹÓõÄϵͳ»·¾³ÎªWindows 2000, Apache 1.3.19£ ......

Python ÖÐÎÄ×Ö·û´®µÄ½ØÈ¡

def
subString
(s,
length):
   
us = unicode(s, 'utf-8
')
    gs =
us.encode('gb2312
')
    n = int(length)
   
t = gs[:n]
    while True
:
        try
:
  &nb ......

ʹÓÃCÓïÑÔÀ©Õ¹Python£¨Ò»£©

¿ª·¢»·¾³£ºUbuntu9.10,python2.6,gcc4.4.11,ubuntuϵÄpythonÔËÐаüºÍ¿ª·¢°üÊÇ·Ö¿ªµÄ£¬Òò´ËÐèÒªÔÚÐÂÀûµÃÀïÃæ°²×°python-all-dev,´Ó¶ø¿ÉÒÔÔÚ´úÂëÖÐÒýÓÃpythonµÄÍ·ÎļþºÍ¿â¡£2.ÏÂÃæÊÇÒ»¸ö×î¼òµ¥µÄ¿ÉÒÔ¹©pythonµ÷ÓõÄcÀ©Õ¹Ä£¿é£¬¼ÙÉèc³ÌÐòÎļþÃûΪfoo.c:´úÂë#include <Python.h>
static PyObject* foo_b ......

ÓÃpy2exe±àÒëPython³ÌÐò

£¨Ò»£© py2exe¼ò½é
      py2exeÊÇÒ»¸ö½«python½Å±¾×ª»»³ÉwindowsÉϵĿɶÀÁ¢Ö´ÐеĿÉÖ´ÐгÌÐò(*.exe)µÄ¹¤¾ß£¬ÕâÑù£¬Äã¾Í¿ÉÒÔ²»ÓÃ×°python¶øÔÚ
windowsϵͳÉÏÔËÐÐÕâ¸ö¿ÉÖ´ÐгÌÐò¡£
¡¡¡¡
¡¡¡¡py2exeÒѾ­±»ÓÃÓÚ´´½¨
wxPython,Tkinter,Pmw,PyGTK,pygame,win32com
clientºÍserver,ºÍÆäËüµÄ¶ÀÁ¢³ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ