python读写文件——生成 数据集的 test.flist 文件 ——【实用教程】
        【摘要】 
                    
                        
                    
                    前言: 
 
   首先相信打开这个博文的大部分是新人,个人建议,遇到陌生的知识点,可直接去教程类文档查阅,而不是看各种博文,高效收集有用资料也是一种技能; 
 
 
 例如本节代码来源: Python ...
    
    
    
    前言:
首先相信打开这个博文的大部分是新人,个人建议,遇到陌生的知识点,可直接去教程类文档查阅,而不是看各种博文,高效收集有用资料也是一种技能;
例如本节代码来源: Python 文件I/O 菜鸟教程 --新人推荐–{ 墨理学AI }
# -*- coding: UTF-8 -*-
 
# 打开一个文件
fo = open("foo.txt", "w")
fo.write( "www.runoob.com!\nVery good site!\n")
 
# 关闭打开的文件
fo.close()
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
示例一:

- genFileList.py 代码如下:
# -*- coding: utf-8 -*-
"""
生成 数据路径 flist 文件
内容示例:
celebahq/0.jpg
celebahq/1.jpg
celebahq/2.jpg
"""
import os
import shutil
flist = []
fo = open("test.flist", "w")
for i in range(10):
    path = 'celebahq/' + str(i) +'.jpg'
    fo.write(path + '\n')
    # flist.append(path)
# print(flist)
fo.close()
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
示例二:

如:
 
# -*- coding: utf-8 -*-
"""
生成 数据路径 flist 文件
数据父目录只有一层
生成内容示例:
celebahq/hello.jpg
celebahq/welcome.jpg
celebahq/to.jpg
celebahq/my.jpg
celebahq/heart.jpg
"""
import glob
import os
flist = []
fo = open("train.flist", "w")
filePath = 'train/'
# 拿到所有路径
fileList = glob.glob(filePath + '*.jpg')
for path in fileList:
    basename = os.path.basename(path)
    temp_path = filePath + basename
    fo.write(temp_path + '\n')
    flist.append(temp_path)
#
print(flist)
fo.close()
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
加油,年轻人 – 墨理

文章来源: positive.blog.csdn.net,作者:墨理学AI,版权归原作者所有,如需转载,请联系作者。
原文链接:positive.blog.csdn.net/article/details/109583021
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)