python os.path.isfile函数
最近刚开始学习Python,做了个小练习:扫描当前目录及其子目录中的文件,找出文件名中含有指定关键字的文件并打印文件名。思路很简单,如果是文件则判断是否满足条件;如果是目录则进入目录搜索文件,递归。
思路有了那就准备开撸
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import sys def search(token):
for file in os.listdir('.'):
if os.path.isfile(file): #如果是文件
if token in file:
print file
elif os.path.isdir(file): #如果是目录
os.chdir(file)
search(token) if __name__ == '__main__':
search(sys.argv[1])
撸完测试,发现结果跟想象的不一样,遇到子目录后就退出了,通过调试发现递归函数返回后再次判断file是否为文件时,结果都是否,后来修改了os.path.isfile函数的参数:在for循环前利用os.getcwd函数获取当前路径并保存,再结合os.path.join函数拼接file形成绝对路径,这样修改后,程序运行结果符合预期。
通过比对分析,问题应该出在os.path.isfile函数处,官方文档上对该函数的说明也比较简略:
os.path.isfile(path)
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
既然对于绝对路径的判断该函数返回结果没问题,而代码中传入的参数是相对(当前)路径的,那说明os.path.isfile函数在处理相对路径的文件时,默认是当前工作目录,恰好代码中再递归调用search函数时修改了当前工作目录,这就导致递归返回后os.path.isfile函数判断结果与预期不符,修改后代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*- import os
import sys def search(token):
pwd = os.getcwd()
for file in os.listdir('.'):
if os.path.isfile(file): #如果是文件
if token in file:
print file
elif os.path.isdir(file): #如果是目录
os.chdir(file)
search(token)
os.chdir(pwd)
if __name__ == '__main__':
search(sys.argv[1])
结论
在调用os.path.isfile函数判断是否为普通文件时,若传入的路径是相对路径,则默认是在当前工作目录下搜索该文件。
python os.path.isfile函数的更多相关文章
- Python os.path模板函数
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
- python中由于中文路径引起的os.path.isfile(imgpath) == False问题
昨天在用python脚本处理文件的时候,遇到了题述问题,明明文件时存在的,但是在用os.path.isfile(imgpath) == False进行判断的时候总是成立,在一开始以为是正反斜杠wind ...
- Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数
Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...
- python中spilt()函数和os.path.spilt()函数区别
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- python 中的split()函数和os.path.split()函数
Python中有split()和os.path.split()两个函数: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表. os.path.split():将文件 ...
- 【Python】os.path.isfile()的使用方法汇总
方法一: # -*- coding:utf-8 -*- import os import sys from uiautomator import device as d filepath = r'E: ...
- python路径拼接os.path.join()函数的用法
os.path.join()函数:连接两个或更多的路径名组件 1.如果各组件名首字母不包含’/’,则函数会自动加上 2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃 3.如果最后一个组 ...
- Python OS模块常用函数说明
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...
- python os.path
os.path 提供了一些处理文件路径的函数. os.path.abspath(path) 返回绝对路径, 在大多数平台上, os.path.abspath(path) == os.path.norm ...
随机推荐
- Oracle触发bug(cursor: mutex S),造成数据库服务器CPU接近100%---SQL子游标多版本问题
问题现象: 项目反馈系统反应非常缓慢,数据库服务器CPU接近100%! INSERT INTO GSPAudit1712(ID,TypeID,CategoryID,DateTime,UserID,Us ...
- cannal&otter源码解析
一点引用资料的整理 http://www.tianshouzhi.com/api/tutorials/canal/381 canal 同步工具 https://github.com/alibaba/c ...
- MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed
MySQL 设置root密码报错:mysqladmin: connect to server at 'localhost' failed 1.安装完MySQL设置root密码报错如下 [root@vm ...
- enable-ssh-key-logon-disable-password-password-less-logon-centos/
cat ~/.ssh/id_rsa.pub | ssh root@destination_server_address "cat >> ~/.ssh/authorized_key ...
- WMware 中CentOS系统Hadoop 分布式环境搭建(一)——Hadoop安装环境准备
1.创建3台虚拟机并装好系统,这里使用64位CentOS. 2.Ping测试[确保两两能ping通]: [ping xxx.xxx.xxx.xxx] 3.安装SSH:[yum install ssh ...
- Mvc 提交表单的4种方法
一,MVC HtmlHelper方法 1. Html.BeginForm(actionName,controllerName,method,htmlAttributes){} 2. ...
- go语言学习--内核态和用户态(协程)
go中的一个特点就是引入了相比于线程更加轻量级的协程(用户态的线程),那么什么是用户态和内核态呢? 一.什么是用户态和内核态 当一个任务(进程)执行系统调用而陷入内核代码中执行时,我们就称进程处于内核 ...
- canvas一些属性
lineTo(x,y) 定义线条结束坐标 moveTo(x,y) 定义线条开始坐标 ctx.stroke();绘制空心图形 ctx.fill();填充图形 把当前路径环绕起来的区域进行填充 ctx.f ...
- webview之学习文章(待续)
webview与js交互: Tencent/VasSonic(缓存优化方案) lzyzsd/JsBridge: pengwei1024/JsBridge: -----webview的框架 TheFin ...
- Tesseract训练
最近在用Tesseract做一个图片识别的小应用,目标图像只有数字和英文字母,在实际使用过程中发现个别数识别错误,因此不得不研究学习Tesseract的训练. http://www.cnblogs.c ...