python之文件I/O

open(file, mode='r', encoding=None, ... ... )  #打开文件,返回file对象
file.close()  #关闭文件
file.read([size])  #读取指定字节数
file.readline([size])  #读取整行,包括“ ”字符
file.readlines([sizeint])  #读取所有行并返回列表
file.write(str)  #将字符串写入文件
file.writelines(sequence)  #向文件写入一个序列字符串列表(换行需自己加入换行符)
 
例如:
with open (path_in, 'r', encoding='UTF-8') as f:
  for line in f.readlines():
    line = line.split()
    head = line[0]
 
用numpy也可以直接读入为numpy.array:
X = np.genfromtxt(file name, delimiter=’ ’)
 
参考:
http://www.runoob.com/python/file-methods.html
原文地址:https://www.cnblogs.com/sbj123456789/p/9917590.html