Ⅰ 如何用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的框架太少了 服务支持也不怎样,打击了吧。不过是实话。