今天写了一个程序,读取子目录(source)下的所有文件,然后转换。

程序一部分代码如下:

def DtoTab(dsrc, dtarget):
try:
for item in os.listdir(dsrc):
if os.path.isfile(item):
print('ok')

然后发现,找不到文件。

最后发现,item读取出来的额,只是文件名,而isfile判断的时候,就在py的workdir下面寻找,所以,失败。修改如下:

def DtoTab(dsrc, dtarget):
try:
for item in os.listdir(dsrc): fullfilename=os.path.join(dsrc,item)
if os.path.isfile(fullfilename) and item.endswith('.txt'):
print('开始处理文件:{}'.format(item))
dfilename = os.path.join(dtarget,item)
ftarget = codecs.open(dfilename, 'w', encoding='utf-8')
icount = 0
for line in open(fullfilename, encoding='utf8'):
line = line.replace(',', '\t')
icount = icount + 1
ftarget.write(line)
ftarget.close()
print('处理完毕文件:{};总计{}行'.format(item, icount)) except Exception as e:
print('出错啦,错误信息是:{}'.format(traceback.format_exc()))

os.path.isfile的错误的更多相关文章

  1. os.path.isfile()的正确用法(正确用法)

    之前网上查找os.path.isfile( )的使用:发现有些是错误的,主要原因是,传入的参数是相对路径,不是绝对路径. 但是,经过我的实验发现:os.path.isfile( )需要传入的参数是绝对 ...

  2. python os.path.isfile函数

    最近刚开始学习Python,做了个小练习:扫描当前目录及其子目录中的文件,找出文件名中含有指定关键字的文件并打印文件名.思路很简单,如果是文件则判断是否满足条件:如果是目录则进入目录搜索文件,递归. ...

  3. python中由于中文路径引起的os.path.isfile(imgpath) == False问题

    昨天在用python脚本处理文件的时候,遇到了题述问题,明明文件时存在的,但是在用os.path.isfile(imgpath) == False进行判断的时候总是成立,在一开始以为是正反斜杠wind ...

  4. 【Python】os.path.isfile()的使用方法汇总

    方法一: # -*- coding:utf-8 -*- import os import sys from uiautomator import device as d filepath = r'E: ...

  5. arcpy加载mxd文件时,无效的MXD路径,提示assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")

    无效的MXD路径,将路径前加‘u’,改为这种: mxdPath = u"C:\\1331\\DB\\Original Files\\dd.mxd" 参考: https://gis. ...

  6. python os.path模块

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  7. os.path 大全

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回一个路径的最后一个组成部分 os.path.commonprefix(list) #返回 ...

  8. Python 中 os.path模板

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

  9. python os.walk()和os.path.walk()

    一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...

随机推荐

  1. 3,版本控制git-多人协作

    如果你想获得一份已经存在了的 Git 仓库的拷贝,比如说,你想为某个开源项目贡献自己的一份力,这时就要用到 `git clone` 命令. 如果你对其它的 VCS 系统(比如说Subversion)很 ...

  2. 通过slf4j/log4j的MDC/NDC 实现日志追踪

    在分布式系统或者较为复杂的系统中,我们希望可以看到一个客户请求的处理过程所涉及到的所有子系统\模块的处理日志. 由于slf4j/log4j基本是日志记录的标准组件,所以slf4j/log4j成为了我的 ...

  3. 斐波那契数列的三种C++实现及时间复杂度分析

    本文介绍了斐波那契数列的三种C++实现并详细地分析了时间复杂度. 斐波那契数列定义:F(1)=1, F(2)=1, F(n)=F(n-1) + F(n-2) (n>2) 如何计算斐波那契数 F( ...

  4. php 链接转二维码图片

    // 类库下载地址 https://sourceforge.net/projects/phpqrcode/files/ $value = 'www.baidu.com';//二维码内容 $errorC ...

  5. shell之正则表达式

    正则表达式(regular expression ,REGEXP): 元字符: .:匹配任意单个字符 []:匹配指定范围内的任意单个字符 [^]:匹配指定范围外的任意字符 字符集合:使用[字符集合] ...

  6. [oldboy-django][2深入django]点击刷新验证码

    # 点击更新验证码,只要重新在发送一个请求即可 <img src="/check_code/" onclick="updateCode(this);" w ...

  7. [UOJ#348][WC2018]州区划分

    [UOJ#348][WC2018]州区划分 试题描述 小 \(S\) 现在拥有 \(n\) 座城市,第ii座城市的人口为 \(w_i\),城市与城市之间可能有双向道路相连. 现在小 \(S\) 要将这 ...

  8. [AtCoder AGC27A]Candy Distribution Again

    题目大意:把$x$个糖果分给$n$个人,必须分完,如果第$i$个人拿到$a_i$个糖果,就会开心,输出最多多少人开心 题解:从小到大排序,判断是否可以让他开心,注意最后判断是否要少一个人(没分完) 卡 ...

  9. 洛谷 最小费用最大流 模板 P3381

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  10. code forces 994C

    C. Two Squares time limit per test 1 second memory limit per test 256 megabytes input standard input ...