python 豎排文本
阿新 • • 發佈:2018-10-02
line __name__ div lose 文件 list popen command lis
新建目錄train,並將目錄data和data1復制到train下
python test data/,data1/
目錄data和data1中包含很多文件,文件中內容都是以空格分隔,將所有文件內容都以空格為分隔符,豎向排列覆蓋到train下相應目錄中
import sys import os def main(argv): arg=sys.argv[1] print arg data_set_list = [] for data_set_num, data_dir in enumerate(arg.split(","), 1): command="ls -l %s |awk ‘NR==2,NR==0 {print $NF}‘" % (data_dir) fp=os.popen(command, "r") ret=fp.readlines() for data_name in ret: data_path=data_dir+data_name[0:-1] data_set_list.append(data_path) print("data_set_list:",data_set_list) for data_index, data_set_name inenumerate(data_set_list): f = open(data_set_name,‘r‘) result=f.read() f.close() o=open(‘train/‘+data_set_name,‘w‘) for i in result.split(‘ ‘): o.write(i) o.write(‘\n‘) o.close if __name__ == "__main__": main(sys.argv[1:])
python 豎排文本