1. 使用print("string", file="")
來實現(xiàn)
with open('./hello','wt') as f:
for i in range(4979):
print("chr{0}\t{1}\t{2}".format(1,i*50000,(i+1)*50000),file=f)
2. 使用sys
來實現(xiàn)
import sys
savedStdout = sys.stdout #保存標準輸出流
with open('./hello', 'wt') as file:
sys.stdout = file #標準輸出重定向至文件
print('This message is for file!')
print("chr{0}\t{1}\t{2}".format(1,i*50000,(i+1)*50000))
sys.stdout = savedStdout #恢復標準輸出流
print ('This message is for screen!')
輸出大文件前幾行(類似R head())
import os
os.chdir("./test")
k=50
with open("85.txt") as f:
n=0
for line in f.readlines():
n+=1
print(line.replace("\n",""))
if n==k:
break