Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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"):
 


Ïà¹ØÎĵµ£º

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

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

pythonÖÐÈçºÎÅжÏÒ»¸ö±äÁ¿µÄÊý¾ÝÀàÐÍ£¿(Ô­´´)

import types
type(x) is types.IntType # ÅжÏÊÇ·ñint ÀàÐÍ
type(x) is types.StringType #ÊÇ·ñstringÀàÐÍ
.........
--------------------------------------------------------
³¬¼¶¶ñÐĵÄģʽ£¬²»ÓüÇסtypes.StringType
import types
type(x) == types(1) # ......

»°ËµPython£¨Áù£©Ð¡°×µÄʤÀû

 
    “¹þ¹þ£¡ÖªµÀÎÒ²Ù×÷Ö®ÍõС°×µÄÀ÷º¦ÁË°É£¡”
    ÿµ±ÖÜÁùÍíÉÏ£¬Ð¡°××ÜÊÇÒª¸úËÞÉáµÄµÜÐÖÌô¼¸¾ÖħÊÞ¡£Æ¾½è×Å·çɧµÄ×ßλºÍâ«ËöµÄÒâʶ£¬Ð¡°××ÜÊÇЦµÀ×îºó¡£Õâ²»£¬Ð¡°×ÓÖÒ»´ÎÈ¡µÃÁËÒÔÒ»µÐ¶þµÄʤÀû¡£
   “Ö÷ÒªÊǶàÏß²Ù×÷£¡”ÕâÊÇС°×¶Ô×Ô¼ºÊ¤ÀûµÄ×ܽ ......

pythonÖеÄlistÈçºÎ½øÐÐÏà¼õ²Ù×÷»òÕß½«list·ÖƬ


ÎÊÌâÃèÊö£º¼ÙÉèÎÒÓÐÕâÑùÁ½¸ölist£¬
          Ò»¸öÊÇlist1£¬list1 = [1, 2, 3, 4, 5]
          Ò»¸öÊÇlist2£¬list2 = [1, 4, 5]
          ÎÒÃÇÈçºÎµÃµ½Ò»¸öеÄlist£ ......

PythonÖÐREÄ£¿éµÄÓ¦ÓÃ

      Python ×Ô1.5°æ±¾ÆðÔö¼ÓÁËre Ä£¿é£¬ËüÌṩ Perl ·ç¸ñµÄÕýÔò±í´ïʽģʽ¡£Python 1.5֮ǰ°æ±¾ÔòÊÇͨ¹ý regex Ä£¿éÌṩ Emecs ·ç¸ñµÄģʽ¡£Emacs ·ç¸ñģʽ¿É¶ÁÐÔÉÔ²îЩ£¬¶øÇÒ¹¦ÄÜÒ²²»Ç¿£¬Òò´Ë±àддúÂëʱ¾¡Á¿²»ÒªÔÙʹÓà regex Ä£¿é£¬µ±È»Å¼¶ûÄ㻹ÊÇ¿ÉÄÜÔÚÀÏ´úÂëÀï·¢ÏÖÆä×ÙÓ°¡£
  & ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ