python os库 搜索_如何用Python os.path.walk方法遍历搜索文件内容的操作详解_

如何用Python os.path.walk方法遍历搜索文件内容的操作详解_

python os使用教程2020-10-08 07:47:18人已围观

如何用Python os.path.walk方法遍历搜索文件内容的操作详解

本文是关于如何用Python os.path.walk方法遍历搜索文件目录内操作详解的,python 代码中用os.path.walk函数这个python模块的方遍历文件,python列出文件夹下的所有文件并找到自己想要的内容。

文中使用到了Python os模块和Python sys模块,这两个模块具体的使法请参考玩蛇网相关文章阅读。

Python os.path.walk方法遍历文件搜索内容方法代码如下:

?

1234567891011121314151617181920212223242526272829303132333435363738394041

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 os模块怎么使用

对于这深入学查找python doc。

www.python.org/docs/

如何学习python的os模块

一、os模块概述

Python os模块包含的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。(一语中的)

二、常用方法

1、os.name

输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。

2、os.getcwd()

函数得到当前工作目录,即当前Python脚本工作的目录路径。

3、os.listdir()

返回指定目录下的所有文件和目录名。

>>> os.listdir(os.getcwd())

['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'Scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']

>>>

4、os.remove()

删除一个文件。

5、os.system()

运行shell命令。

>>> os.system('dir')

0

>>> os.system('cmd') #启动dos

6、os.sep 可以取代操作系统特定的路径分割符。

7、os.linesep字符串给出当前平台使用的行终止符

>>> os.linesep

'\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。

>>> os.sep

'\\' #Windows

>>>

8、os.path.split()

函数返回一个路径的目录名和文件名

>>> os.path.split('C:\\Python25\\abc.txt')

('C:\\Python25', 'abc.txt')

9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。

>>> os.path.isdir(os.getcwd())

True

>>> os.path.isfile('a.txt')

False

10、os.path.exists()函数用来检验给出的路径是否真地存在

>>> os.path.exists('C:\\Python25\\abc.txt')

False

>>> os.path.exists('C:\\Python25')

True

>>>

11、os.path.abspath(name):获得绝对路径

12、os.path.normpath(path):规范path字符串形式

13、os.path.getsize(name):获得文件大小,如果name是目录返回0L

14、os.path.splitext():分离文件名与扩展名

>>> os.path.splitext('a.txt')

('a', '.txt')

15、os.path.join(path,name):连接目录与文件名或目录

>>> os.path.join('c:\\Python','a.txt')

'c:\\Python\\a.txt'

>>> os.path.join('c:\\Python','f1')

'c:\\Python\\f1'

>>>

16、os.path.basename(path):返回文件名

>>> os.path.basename('a.txt')

'a.txt'

>>> os.path.basename('c:\\Python\\a.txt')

'a.txt'

>>>

17、os.path.dirname(path):返回文件路径

>>> os.path.dirname('c:\\Python\\a.txt')

'c:\\Python'

版权声明:本站所有文章皆为原创,欢迎转载或转发,请保留网站地址和作者信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,可以使用os.walk函数来遍历目录结构。os.walk函数会递归地遍历目录结构,包括所有的子目录和文件。如果需要控制遍历深度,可以使用os.walk函数的depth参数来限制遍历的深度。 depth参数表示遍历的深度限制。如果depth参数为None(默认值),则os.walk函数将遍历整个目录结构。如果depth参数为正整数,则os.walk函数将在遍历到指定深度后停止遍历子目录。例如,如果depth参数为1,则os.walk函数将只遍历当前目录,而不会递归遍历子目录。如果depth参数为0,则os.walk函数将不会遍历任何目录。 以下是一个示例代码,演示如何使用os.walk函数的depth参数来控制遍历深度: ``` import os def walk_dir(root_dir, depth=None): for root, dirs, files in os.walk(root_dir): if depth is not None and root.count(os.sep) - root_dir.count(os.sep) >= depth: # 当前目录深度超过指定深度,不再继续递归 continue # 处理当前目录下的文件 for file in files: file_path = os.path.join(root, file) print(file_path) # 处理当前目录下的子目录 for dir in dirs: dir_path = os.path.join(root, dir) print(dir_path) # 递归遍历子目录 walk_dir(dir_path, depth) ``` 在上述示例代码中,`depth`参数用于限制遍历深度。`root_dir`参数表示要遍历的根目录。在`for root, dirs, files in os.walk(root_dir):`这一行代码中,`os.walk`函数开始遍历目录结构。`root`表示当前目录的路径,`dirs`表示当前目录下的子目录列表,`files`表示当前目录下的文件列表。如果当前目录深度超过指定深度,则使用`continue`语句跳过当前目录,不再继续递归遍历。否则,会分别遍历当前目录下的文件和子目录,并递归遍历子目录(如果深度未超过指定深度)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值