Ⅰ 如何用python os.path.walk方法遍歷搜索文件內容的操作詳解
文中使用到了Python os模塊和Python sys模塊,這兩個模塊具體的使用方法請參考玩蛇網相關文章閱讀。
Python os.path.walk方法遍歷文件搜索內容方法代碼如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os, sys
#代碼中需要用到的方法模塊導入
listonly = False
skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files
def visitfile(fname, searchKey):
global fcount, vcount
try:
if not listonly:
if os.path.splitext(fname)[1] in skipexts:
pass
elif open(fname).read().find(searchKey) != -1:
print'%s has %s' % (fname, searchKey)
fcount += 1
except: pass
vcount += 1
#www.iplaypy.com
def visitor(args, directoryName,filesInDirectory):
for fname in filesInDirectory:
fpath = os.path.join(directoryName, fname)
if not os.path.isdir(fpath):
visitfile(fpath,args)
def searcher(startdir, searchkey):
global fcount, vcount
fcount = vcount = 0
os.path.walk(startdir, visitor, searchkey)
if __name__ == '__main__':
root=raw_input("type root directory:")
key=raw_input("type key:")
searcher(root,key)
print 'Found in %d files, visited %d' % (fcount, vcount)
Ⅱ Python多長時間可以學會精通
很多初學者都有這樣的疑問,Python需要多長時間可以學會,達到精通呢?
學會Python需要多長時間?
如果是自學,從零基礎開始學習Python的話,依照每個人理解能力的不同,大致上需要半年到一年半左右的時間,
當然,如果有其它編程語言的經驗,入門還是非常快的,大概需要2~3個月可以對上手Python語言編寫一些簡單的應用。
無論是新手還是有一定基礎的朋友,有一個有經驗的人帶著自己學習,或者參加我們的玩蛇網Python培訓課程,都會1個月左右入門,3個月左右對Python有一個全面系統的了解,達到自己動手編程解決問題的能力。
精通Python需要多長時間?
任何知識都是基礎入門比較快,達到精通的程序是需要時日的,這是一個逐漸激烈的過程。
精通任何一門編程語言,都需要通過大量的實踐來積累經驗,解決遇到的各種疑難問題,看別人的源碼,分享自己的分碼的這個過程,才能夠精通Python的方方面面。從編程的一開始,就應該不斷的動手去編寫代碼,不停的去實踐,不停的去修改,不停的總結經驗,最終才能熟能生巧,達到精通。
一個對Python程序能算的上精通的程序員,對同樣一個問題,他知道很多種解決問題的方法,並能從中選擇最有效率的方法!
Ⅲ 如何用Python os.path.walk方法遍歷搜索文件內容的操作詳解
本文是關於如何用Python os.path.walk方法遍歷搜索文件目錄內容的操作詳解的文章,python 代碼中用os.path.walk函數這個python模塊的方法來遍歷文件,python列出文件夾下的所有文件並找到自己想要的內容。
文中使用到了Python os模塊和Python sys模塊,這兩個模塊具體的使用方法請參考玩蛇網相關文章閱讀。
Python os.path.walk方法遍歷文件搜索內容方法代碼如下:
?
041
import os, sys#代碼中需要用到的方法模塊導入 listonly = False skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files def visitfile(fname, searchKey): global fcount, vcount try: if not listonly: if os.path.splitext(fname)[1] in skipexts: pass elif open(fname).read().find(searchKey) != -1: print'%s has %s' % (fname, searchKey) fcount += 1 except: pass vcount += 1 #www.iplaypy.com def visitor(args, directoryName,filesInDirectory): for fname in filesInDirectory: fpath = os.path.join(directoryName, fname) if not os.path.isdir(fpath): visitfile(fpath,args) def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 os.path.walk(startdir, visitor, searchkey) if __name__ == '__main__': root=raw_input("type root directory:") key=raw_input("type key:") searcher(root,key) print 'Found in %d files, visited %d' % (fcount, vcount)
Ⅳ 請問python主要應用領域是什麼,哪方面用的多了,適合做什麼
我用python就是寫網頁爬蟲;
速度應該還是不錯的;
但是他對中文的支持不咋地 尤其遇到一些編碼轉換的地方 初學時很容易在這里較勁;
你要是會java或.net然後去學它還是不難的,但比java強大好早著呢,別忘了java是誰捧起來的;
最後說下,python做web的框架太少了 服務支持也不怎樣,打擊了吧。不過是實話。