Python 获取文件夹内所有文件名
RDT_list = os. listdir( RDT_path)
path = './VOCdevkit/VOC2007/JPEGImages/*'
xml_paths = glob. glob( path)
Python 将制定文件复制到新的文件夹中
import os
import shutil
RDT_path = 'D:\\test\\RDT_problem'
RDT_list = os. listdir( RDT_path)
RD_path = 'D:\\test\\evaluate_RD_test'
for i in RDT_list:
found_name = os. path. join( RD_path, i)
new_folder = r'D:\test\RD_problem'
shutil. copy( found_name, os. path. join( new_folder, i) )
print ( "copy {} -> {}" . format ( found_name, os. path. join( new_folder, i) ) )
Python 文件重命名
old_dir = RDT_path + '\\' + i
new_dir = RDT_path + '\\' + file_name + '.jpg'
os. rename( old_dir, new_dir)
Python 将数据保存成txt/csv格式
def text_save ( filename, data) :
file = open ( filename, 'a' )
for i in range ( len ( data) ) :
s = str ( data[ i] ) . replace( '[' , '' ) . replace( ']' , '' )
s = s. replace( "'" , '' ) . replace( ',' , '' ) + '\n'
file . write( s)
file . close( )
print ( "保存文件成功" )
Python 随机挑选一定数量数据保存
path = "/data/lshib/MM-DistillNet/dataset/train_all.txt"
ids = get_id_list( path)
sample_num = 20000
text_save( 'plt_test_all.txt' , random. sample( ids, sample_num) )
Python 逐行读取txt文件内容并保存
with open ( "test.txt" , "r" , encoding= 'utf-8' ) as f:
for line in f. readlines( ) :
line = line. strip( '\n' )
name = line. split( '-' ) [ 0 ]
List. append( name)
Python 在txt文件中逐行写入列表数据
with open ( "cc.txt" , "w" , encoding= 'utf-8' ) as f:
for file in List:
f. write( file + '\n' )
f. close( )
Python 切分数据集
def qieshujuji ( xml_paths) :
'''切分数据集'''
total_num = len ( xml_paths)
train_num = int ( 0.7 * total_num)
val_num = total_num - train_num
index = list ( range ( 0 , total_num) )
random. shuffle( index)
train_txt = "train.txt"
with open ( train_txt, 'w' , encoding= "utf-8-sig" ) as f1:
f1. write( "" )
with open ( train_txt, 'a' , encoding= "utf-8-sig" ) as f1:
f1. write( "text_a\tlabel" )
f1. write( "\n" )
for i in range ( 0 , train_num) :
f1. write( xml_paths[ index[ i] ] )
f1. write( "\n" )
test_txt = "test.txt"
with open ( test_txt, 'w' , encoding= "utf-8-sig" ) as f3:
f3. write( "" )
with open ( test_txt, 'a' , encoding= "utf-8-sig" ) as f3:
f3. write( "text_a\tlabel" )
f3. write( "\n" )
for i in range ( train_num, total_num) :
f3. write( xml_paths[ index[ i] ] )
f3. write( "\n" )