os.path.isfile的错误
2017-06-13 14:36
218 查看
今天写了一个程序,读取子目录(source)下的所有文件,然后转换。
程序一部分代码如下:
然后发现,找不到文件。
最后发现,item读取出来的额,只是文件名,而isfile判断的时候,就在py的workdir下面寻找,所以,失败。修改如下:
程序一部分代码如下:
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()))
相关文章推荐
- python os.path.isfile()因参数问题判断错误
- os.path.isfile 使用误区
- os.path.isfile() 判断问题
- 错误提示: The project was not built since its build path is incomplete. Cannot find the class file for j
- Eclipse错误提示:The project was not built since its build path is incomplete. Cannot find the class file
- spring错误处理 Build path is incomplete. Cannot find class file for org.springframework.aop.Advisor
- python中由于中文路径引起的os.path.isfile(imgpath) == False问题
- spring错误处理 Build path is incomplete. Cannot find class file for org.springframework.aop.Advisor
- 使用GreenDao 报"This check is to prevent accidental file generation into a wrong path."错误
- FCKeditor出现"this connector is disabled Please check the"editor/filemanager/connectors/aspx/config.aspx"错误的解决办法
- FCKeditor关于图片上传出现错误“ 提示没有权限 Type=Image” 以及出现"this connector is disabled Please check the"editor/filemanager/connectors/aspx/conf
- Windows Azure - "The Path is too long after being fully qualified" 错误信息的解决方案(转)
- iPhone: There is no SDK with the name or path iphoneos XXX
- 关于系统出现the database file is locked错误的说明
- 打开sln文件出现the selected file is not a valid visual studio solution file错误提示解决办法
- vc6 编辑出现错误“ **.pch is not a precompiled header file created with this compiler"
- FCKeditor出现"this connector is disabled Please check the"editor/filemanager/connectors/aspx/config.aspx"错误的解决办法【转】
- compat.linux.osrelease: 2.4.2 is not supported错误的解决方法
- vc 出现“fatal error C1085: Cannot write precompiled header file: 'Debug/xxx.pch': The parameter is incorrect“的错误的解决办法
- The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object. Fix the build pat