# coding=gbk
import os
import os.path
 
#读取目录下的所有文件,包括嵌套的文件夹
def GetFileList(dir, fileList):
    newDir = dir
    if os.path.isfile(dir):
        fileList.append(dir)
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            # 如果需要忽略某些文件夹,使用以下代码
            # if s == "xxx":
            # continue
            newDir = os.path.join(dir, s)
            GetFileList(newDir, fileList)
    return fileList
 
 
fileDir = "E:\\Differnernt_Size_Digit_Data\\ReSize\\Train\\28x28"
list = GetFileList(fileDir, [])
# 打开一个文件
fo = open("file_list.txt", "w")  # 打开文件
for i in list:
    print(i)  # 测试完整文件路径
    print(os.path.basename(i))  # 文件名
    index = i.find(".", 0)  # 找到点号的位置
    print(i[index - 1:index])  # 截取目标字符
    print(os.path.basename(i) + " " + i[index - 1:index])  # 测试目标字符串
    fo.write(os.path.basename(i) + " " + i[index - 1:index] + "\n")  # 将目标字符串写入文件
fo.close()  # 关闭打开的文件

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Python 读取文件下所有内容、获取文件名、截取字符、写回文件的更多相关文章

  1. struts文件上传,获取文件名和文件类型

    struts文件上传,获取文件名和文件类型   Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...

  2. shell脚本获取文件名、路径名、文件类型

    1. 从字符串获取指定内容 从字符串中提取特定的信息,常用于获取文件名.文件类型.所在路径等. 1.1 获取字符串信息 用指定的方式(PATTERN)从字符串(PARAMETERS)中移除内容 &qu ...

  3. Python开发【笔记】:从海量文件的目录中获取文件名--方法性能对比

    Python获取文件名的方法性能对比 前言:平常在python中从文件夹中获取文件名的简单方法   os.system('ll /data/')   但是当文件夹中含有巨量文件时,这种方式完全是行不通 ...

  4. Python读取word文档内容

    1,利用python读取纯文字的word文档,读取段落和段落里的文字. 先读取段落,代码如下: 1 ''' 2 #利用python读取word文档,先读取段落 3 ''' 4 #导入所需库 5 fro ...

  5. dialogs打开对话框选定文件夹,getopenfilename获取文件名

    如果需要使用“打开”.“打印”等Excel内置对话框已经具有的功能,可以使用代码直接调用这些内置的对话框,如下面的代码所示. #001  Sub DialogOpen() #002      Appl ...

  6. 【转】Python——读取html的table内容

    Python——python读取html实战,作业7(python programming) 查看源码,观察html结构 # -*- coding: utf-8 -*- from lxml.html ...

  7. linux下C++遍历文件夹下的全部文件;Windows/Linux下C++批量修改文件名,批量删除文件

    Linux下 C++遍历目录下所有文件 rename(image_path.c_str(), image_path_new.c_str()); remove(image_path_move.c_str ...

  8. linux shell 删除指定文件夹下面 名称不包含指定字符的文件

    find /app/jenkins/jenkins/jobs/scam/* ! -name config.xml | xargs rm -rf 删除/app/jenkins/jenkins/jobs/ ...

  9. python 读取目录下的文件

    参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...

随机推荐

  1. OpenMp之false sharing

    关于false sharing的文章,网上一大堆了,不过觉得都不太系统,那么下面着重系统说明一下. 先看看外国佬下的定义: In symmetric multiprocessor (SMP) syst ...

  2. [HZNUOJ1524]排队买票(DP)

    题目链接:http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1524 简单分析后可以知道每一个手持两元的小朋友前面,售票员手里至少有一个一元. 假设d ...

  3. selenium-webdriver(python) (十) 如何处理下拉框

    本节重点 处理下拉框 switch_to_alert() accept() 下拉框是我们最常见的一种页面元素,对于一般的元素,我们只需要一次就定位,但下拉框里的内容需要进行两次定位,先定位到下拉框,再 ...

  4. List排序的两种简便方式

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace List ...

  5. 淘宝主搜索离线集群完成Hadoop 2

    淘宝搜索离线dump集群(hadoop&hbase)2013进行了几次重大升级,本文中将这些升级的详细过程.升级中所遇到的问题以及这些问题的解决方案分享给大家.至此,淘宝主搜索离线集群完全进入 ...

  6. 重装yum

    1)卸载yum rpm -aq|grep yum|xargs rpm -e --nodeps 2)下载并安装python-urlgrabber,python-pycurl,yum-metadata-p ...

  7. ecshop 更新首页flash样式

    未测试 ECSHOP默认的只有几种很普通的FLASH图片切换样式,想不想自己也换一种呢? 今天摸索了下,算是弄懂了,和大家分享下 首先在网上找到你想要的FLASH切换样式[google一下],把那个S ...

  8. poj 1986 Distance Queries

    好像是模板题  当作练习题 不错:  要求任意两点之间的距离.可以假设一个根节点,然后所有点到根节点的距离,然后求出任意两点多公共祖先:  距离就变成了 dis[u]+dis[v] - 2*dis[ ...

  9. 【转】Linux设备驱动之mmap设备操作

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/08/2281222.html 1.mmap系统调用 void *mmap(void *addr, ...

  10. delete archivelog all 无法彻底删除归档日志?

    最近在因归档日志暴增,使用delete archivelog all貌似无法清除所有的归档日志,到底是什么原因呢? 1.演示环境 SQL> select * from v$version whe ...