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

pythonÎļþºÍ·¾¶²Ù×÷½éÉÜ

from http://blog.alexa-pro.cn/?p=189
1: os.listdir(path) //pathΪĿ¼
¹¦ÄÜÏ൱ÓÚÔÚpathĿ¼ÏÂÖ´ÐÐdirÃüÁ·µ»ØΪlistÀàÐÍ
print os.listdir('..')
Êä³ö£º
[a,b,c,d]
2: os.path.walk(path,visit,arg)
path £ºÊǽ«Òª±éÀúµÄĿ¼
visit £ºÊÇÒ»¸öº¯ÊýÖ¸Õ룬º¯ÊýÔ²ÐÎΪ£º
callback(arg,dir,fileList)
ÆäÖÐargΪΪ´«¸øwalkµÄarg £¬ dirÊÇpathϵÄÒ»¸öĿ¼£¬fileListΪdirϵÄÎļþºÍĿ¼×é³ÉµÄlist
arg£º´«¸øvisitÓõģ¬¶ÔwalkûÓÐʲô×÷ÓÃ
¾ÙÀý£º
def callback(arg,directory, files):
print directory,
print files,
print arg
print '——————–'
os.path.walk('.',callback, '123456′)
Êä³ö£º
. ['path0704.py', 'temp', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'] 123456
——————–
.\temp ['temp.h', 'temp1'] 123456
——————–
.\temp\temp1 ['abc.bmp'] 123456
Èç¹ûÏëÕÒµ½Ä³¸öĿ¼ÏÂËùÓÐÎļþ£¬Ö»ÐèÒªÔÚcallbackÀïÃ棬ÔÚfileListÖÐÕÒ³öÎļþ£¬¼´¿É³ý´ËÖ®Í⣬»¹ÓÐÒ»¸öº¯Êý¿ÉÒÔÓÃÄǾÍÊÇos.walk£¬¿´10
3£ºos.path.split(path)
path Ϊһ¸ö·¾¶£¬
Êä³ö£¬°Ñpath·Ö³ÉÁ½²¿·Ö£¬¾ßÌ忴ʵÀý£º
print os.path.split("abc/de.txt")
('abc', 'de.txt')
os.path.split("abc")
(", 'abc')
print os.path.split("de/abc/de")
('de/abc', 'de')
4: os.path.splitext(filename)
°ÑÎļþÃû·Ö³ÉÎļþÃû³ÆºÍÀ©Õ¹Ãû
os.path.splitext(abc/abcd.txt)
('abc/abcd', '.txt')
5: os.path.dirname(path)
°ÑĿ¼ÃûÌá³öÀ´
print os.path.dirname("abc")
#Êä³öΪ¿Õ
print os.path.dirname('abc\def')
abc
6£º os.path.basename(filename)
È¡µÃÖ÷ÎļþÃû
print os.path.basename('abc')
abc
print os.path.basename('abc.txt')
abc
print os.path.basename('bcd/abc')
abc #Õâ¸öÐèҪעÒâ²»°üÀ¨Ä¿Â¼Ãû³Æ
print os.path.basename('.')
.
7£ºos.mkdir(path, [mode])
pathΪĿ¼Ãû£º ÕâÀïÓиöÒªÇó£¬Ö»ÄÜ´´½¨Ò»¼¶Ä¿Â¼
±ÈÈçpathΪ abc/def Ôòµ±Ç°Ä¿Â¼Ï±ØÐë´æÔÚabc ·ñÔòʧ°Ü
8£º os.makedirs(path [,mode])
¿ÉÒÔ´´½¨¶à¼¶Ä¿Â¼
9£ºos.remove(path)ɾ³ýÒ»¸öÎļþ£¬Ò»¶¨ÊÇÒ»¸öÎļþ
os.removedirs(path) ɾ³ýÒ»¸öĿ¼ÏÂËùÓж«Î÷
os.rmdir(path) ɾ³ýÒ»¸öĿ¼£¬¶øÇÒÒ»¶¨Òª¿Õ£¬·ñÔ


Ïà¹ØÎĵµ£º

ʹÓÃCÓïÑÔÀ©Õ¹Python(ËÄ)

ÉÏһƪÀïµÄLAMEÏîÄ¿ÒѾ­Õ¹Ê¾ÁËpythonÈçºÎÓëCÓïÑÔ½»»¥£¬µ«³ÌÐòÈÔ²»¹»ÀíÏ룬ÔÚpythonÕâÒ»¶Ë½ö½öÊÇ´«µÝÔ´ÎļþºÍÄ¿±êÎļþµÄ·¾¶£¬ÔÙµ÷ÓÃCÄ£¿éµÄencode·½·¨À´½øÐбàÂ룬µ«ÎÊÌâÔÚÓÚÄãÎÞ·¨¿ØÖÆencodeº¯Êý£¬±ÈÈçÄãÏë±àÂëµÄÔ´ÎļþÈç¹û²»ÊÇԭʼÊý¾Ý£¬¶øÊÇwavÎļþ»òÕßÆäËû¸ñʽÄØ£¿¶ÔÓÚÕâ¸öÎÊÌ⣬ÓÐÁ½ÖÖ·½·¨¿ÉÒÔÑ¡Ôñ£¬Ò»ÖÖÄ£·ÂÇ°ÃæµÄCÄ£ ......

pythonÖÐeval, exec, execfile,ºÍcompile


eval(str [,globals [,locals ]])º¯Êý½«×Ö·û´®strµ±³ÉÓÐЧPython±í´ïʽÀ´ÇóÖµ£¬²¢·µ»Ø¼ÆËã½á¹û¡£
ͬÑùµØ, execÓï¾ä½«×Ö·û´®strµ±³ÉÓÐЧPython´úÂëÀ´Ö´ÐÐ.Ìṩ¸øexecµÄ´úÂëµÄÃû³Æ¿Õ¼äºÍexecÓï¾äµÄÃû³Æ¿Õ¼äÏàͬ.
×îºó£¬execfile(filename [,globals [,locals ]])º¯Êý¿ÉÒÔÓÃÀ´Ö´ÐÐÒ»¸öÎļþ,¿´ÏÂÃæµÄÀý×Ó:
>>> ev ......

python ÃüÁîÐвÎÊý


±¾Æª½«½éÉÜpythonÖÐsys, getoptÄ£¿é´¦ÀíÃüÁîÐвÎÊý
Èç¹ûÏë¶Ôpython½Å±¾´«²ÎÊý£¬pythonÖжÔÓ¦µÄargc, argv(cÓïÑÔµÄÃüÁîÐвÎÊý)ÊÇʲôÄØ£¿
ÐèҪģ¿é£ºsys
²ÎÊý¸öÊý£ºlen(sys.argv)
½Å±¾Ãû£º    sys.argv[0]
²ÎÊý1£º     sys.argv[1]
²ÎÊý2£º     sys.argv[2]
test.py
1
import ......

ÔÚpythonÖд¦ÀíXML×ܽá

±¾ÎÄ×ܽáÁËÔÚPythonÖÐÖ÷ÒªµÄ¼¸ÖÖ´¦ÀíXMLµÄ·½·¨£º
Element Tree
SAX
Dom
ʹÓõÚÈý·½Àà¿âÈçAmara 2.x£¬ libxml2dom µÈ
Element Tree
Element TreeÊÇPython 2.5ÒýÈëµÄʹÓüòµ¥£¬¿ì½ÝµÄ´¦Àí·½·¨£¬ Ò²ÊÇPython±ê×¼Àà¿âÍƼöµÄ´¦ÀíXMLµÄ·½·¨¡£
ËüʹÓÃÊ÷ÐνڵãµÄÐÎʽÀ´¶ÁдXML¡£Element TreeÊÇÇáÁ¿¼¶µÄDOM£¬ ËùÒÔÊ ......

zzʹÓà Python ·ÖÀëÖÐÎÄÓëÓ¢ÎĵĻìºÏ×Ö´®

ʹÓà Python ·ÖÀëÖÐÎÄÓëÓ¢ÎĵĻìºÏ×Ö´®
LiYanrui

posted @ ´óÔ¼ 1 ÄêÇ°
in ³ÌÐòÉè¼Æ
with tags
python

, 614 ÔĶÁ


Õâ¸öÎÊÌâÊÇ×ö MkIV Ô¤´¦Àí³ÌÐò
ʱ¸ã¶¨µÄ£¬¾ÍÊÇ°ÑÒ»¸ö»ìºÏÁËÖÐÓ¢ÎÄ»ìºÏ×Ö´®·ÖÀëΪӢÎÄÓëÖÐÎĵÄ×Ó×Ö´®£¬Æ©È磬½« ”ÎÒµÄ English ѧµÄ²»ºÃ
&ld ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ