Python 关于txt文件操作

一、打开文件

f = open(filename,access_mode='r',buffering=-1)
   
   
  • 1

filename:文件名
access_mode:打开方式,r读,w写,a追加,r+ w+ a+ 都是以读写方式打开,rb二进制读,wb二进制写,rb+ wb+ ab+二进制读写
buffering:默认值
二、对文件进行操作
将文件中的内容读入到一个字符串变量/列表中
函数:read(),readline(),readlines(),write(),writelines()
1、read() 读取整个文件到字符串变量中
2、readline() 读取文件中的一行,然后返回整行,包括行结束符到字符串变量中
3、readlines() 读取整个文件,返回一个字符串列表,列表中的每个元素都是一个字符串,代表一行

fp=open('filename.txt')
lines = fp.readlines()
for line in lines:
...
fp.close()

######更高效的实现方式########
fp = open('filename.txt')
for line in fp:
...
fp.close()
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、write() 将字符串输出到文件中

f = open('filename,txt','w')
f.write('welcome to my house')
f.close()
########
f = open('filename,txt','w')
f.write('welcome\nto\nmy\nhouse')
f.close()
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5、writelines() 将字符串列表写入文件,行结束符并不会自动被加入,需要手动在每行的结尾加入行结束符。

f = open('filename,txt','w')
w = ['hello', 'world']
f.writelines(w)
f.close()
###########
f = open('filename,txt','w')
w = ['hello\n', 'world']
f.writelines(w)
f.close()
################
f = open('filename,txt','w')
f.write('firstline\n')
f.write('secondline\n')
f.write('thirdline\n')
f.close()
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

6、seek() 移动文件读取指针到指定的位置
f.seek(p,0) 移动当文件第p个字节处,绝对位置
f.seek(p,1) 移动到相对于当前位置之后的p个字节
f.seek(p,2) 移动到相对文章尾之后的p个字节
7、tell() 返回文件指针的位置

MTCNN复现程序中,单个图片检测中的一段与txt文件相关的程序

path = "/home/kk17/PythonProject/dataset/FDDB/FDDB-folds/"
prefixpath = "/home/kk17/PythonProject/dataset/FDDB/originalPics/"
#for item in os.listdir(path):  # listdir(file) 获取file下的目录和文件
#    gt_imdb.append(os.path.join(path, item))  # 目录组合起来, 第一个绝对路径前面的参数忽略,即获取图像路径
results = open('results.txt', 'w') #写入测试结果的.txt文件
for i in range(1,11):
    foldfilename = "FDDB-fold-%02d.txt" % i
    foldfilename = path + foldfilename
    #print foldfilename
    foldfile = open(foldfilename, 'r') #只读打开一个 FDDB-fold-i.txt文件

    for imagepath in foldfile.readlines():  #读取一行,这里是readlines!有个s
        #print imagepath
        imagepath = imagepath.split('\n')[0] #因为readlines所以包括'\n',这样可以去掉回车符

        results.write(str(imagepath) + '\n')# 向 result.txt 中写入图片路径
        img = prefixpath + imagepath + '.jpg'
        pathlist = [img] #  包含路径的列表, 但只有一个元素, 因为作者写的 TestLoader() 需要接受列表
        #print imagepath
        test_data = TestLoader(pathlist) # 加载一张图片
        boundingboxes, landmarks = mtcnn_detector.detect_face(test_data)# 检测图片的输出, 类似 pathlist, 列表 boundingboxes 中也只包含一个元素
        results.write(str(len(boundingboxes[0])) + '\n') #检测到的人脸个数
        for boundingbox in boundingboxes[0]:  # 向 result.txt 中写入检测出的人脸的位置信息和置信度
            results.write(str(int(boundingbox[0])) + ' ' + str(int(boundingbox[1])) + ' ' \
                          + str(int(boundingbox[2] - boundingbox[0])) + ' ' \
                          + str(int(boundingbox[3] - boundingbox[1])) + ' ' \
                          + str(boundingbox[4]) + '\n')
            # print 'Done'
    foldfile.close()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值