Python 读取文件下所有内容、获取文件名、截取字符、写回文件
# 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 读取文件下所有内容、获取文件名、截取字符、写回文件的更多相关文章
- struts文件上传,获取文件名和文件类型
struts文件上传,获取文件名和文件类型 Action中还有两个属 性:uploadFileName和uploadContentType,这两个属性分别用于封装上传文件的文件名.文件类型.这是S ...
- shell脚本获取文件名、路径名、文件类型
1. 从字符串获取指定内容 从字符串中提取特定的信息,常用于获取文件名.文件类型.所在路径等. 1.1 获取字符串信息 用指定的方式(PATTERN)从字符串(PARAMETERS)中移除内容 &qu ...
- Python开发【笔记】:从海量文件的目录中获取文件名--方法性能对比
Python获取文件名的方法性能对比 前言:平常在python中从文件夹中获取文件名的简单方法 os.system('ll /data/') 但是当文件夹中含有巨量文件时,这种方式完全是行不通 ...
- Python读取word文档内容
1,利用python读取纯文字的word文档,读取段落和段落里的文字. 先读取段落,代码如下: 1 ''' 2 #利用python读取word文档,先读取段落 3 ''' 4 #导入所需库 5 fro ...
- dialogs打开对话框选定文件夹,getopenfilename获取文件名
如果需要使用“打开”.“打印”等Excel内置对话框已经具有的功能,可以使用代码直接调用这些内置的对话框,如下面的代码所示. #001 Sub DialogOpen() #002 Appl ...
- 【转】Python——读取html的table内容
Python——python读取html实战,作业7(python programming) 查看源码,观察html结构 # -*- coding: utf-8 -*- from lxml.html ...
- linux下C++遍历文件夹下的全部文件;Windows/Linux下C++批量修改文件名,批量删除文件
Linux下 C++遍历目录下所有文件 rename(image_path.c_str(), image_path_new.c_str()); remove(image_path_move.c_str ...
- linux shell 删除指定文件夹下面 名称不包含指定字符的文件
find /app/jenkins/jenkins/jobs/scam/* ! -name config.xml | xargs rm -rf 删除/app/jenkins/jenkins/jobs/ ...
- python 读取目录下的文件
参考方法: import os path = r'C:\Users\Administrator\Desktop\file' for filename in os.listdir(path): prin ...
随机推荐
- Python代码编辑器
PyCharm Community 说到PyCharm Community,我们就会想到它是一款免费的开源Python代码编辑器,不过这也是事实啦.PyCharm Community为我们提供了轻量级 ...
- [置顶] Android Provision (Setup Wizard)
Android中很多框架性的设计都已经存在了,但在市场上的发布版本里却因为没有很好的理解Android的设计意图而进行自己的定制,或者自己又做一 个冗余的实现.Android中的Provision其实 ...
- eclipse有生成不带参数的构造方法的快捷键吗
你打上类名的2个字母,然后”alt“ +“/” 基本上选第一个就行了
- Machine Learning for hackers读书笔记(六)正则化:文本回归
data<-'F:\\learning\\ML_for_Hackers\\ML_for_Hackers-master\\06-Regularization\\data\\' ranks < ...
- LA 2678 Subsequence
有一个正整数序列,求最短的子序列使得其和大于等于S,并输出最短的长度. 用数组b[i]存放序列的前i项和,所以b[i]是递增的. 遍历终点j,然后在区间[0, j)里二分查找满足b[j]-b[i]≥S ...
- 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?
Java time JavaScript Math.round(new Date().getTime()/1000) 之所以除以1000是因为getTime()返回数值的单位是毫秒 Microsoft ...
- swift2.0 Cannot assign a value of type '[CFString]' to a value of type '[String]'
Cannot assign a value of type '[CFString]' to a value of type '[String]' 代码示例如下: picker.mediaTypes = ...
- python练习程序(c100经典例12)
题目: 判断101-200之间有多少个素数,并输出所有素数. for i in range(101,201): flag=0; for j in range(2,int(i**(1.0/2))): i ...
- 【英语】Bingo口语笔记(14) - 表示“不愉快”
bail on 放弃;背弃
- 【英语】Bingo口语笔记(26) - Take系列
raincheck 改日 take .. off 在这里是请假的意思