I am having a bit of an issue printing my python output to a txt file. I am currently running a script as such:
import glob import pandas as pd files = glob.glob('*.txt') result = pd.DataFrame() for file in files: df = pd.read_csv(file,delimiter=' ') current_col = df.columns[0] df.reset_index(inplace=True) df.set_index(current_col,inplace=True) df.index.name = 'index' df.rename(columns={'index':current_col}, inplace=True) result = pd.concat([result,df],axis=1) I then eventually get the output I am looking for with:
>>> result >>> result.fillna(0) But I would like to get these results to print to a txt file. I am currently running these commands in the terminal on a mac with python v3.7.4. I have tried opening a text file and writing the results to the text file with no success:
import glob import pandas as pd file = open('counts.txt','w') files = glob.glob('*.txt') #eg: files = ['1.txt','2.txt'] result = pd.DataFrame() for file in files: df = pd.read_csv(file,delimiter=' ') current_col = df.columns[0] df.reset_index(inplace=True) df.set_index(current_col,inplace=True) df.index.name = 'index' df.rename(columns={'index':current_col}, inplace=True) result = pd.concat([result,df],axis=1) file.write(result +'\n')