1

I am recording output of my script in a 2 different files, Dailydata.txt and historicData.txt. I was hoping that I need to just stdout in 2 different files like below,

sys.stdout = open('historicData.txt', 'a+') sys.stdout = open('Dailydata.txt', 'r+') 

But, it directs output to one file only.

So I redirected my output to DailyData.txt first and then write it in HistoricData.txt

 with open(file_path + 'HistoricDaily.txt', 'r') as fread, open(file_path + 'FidelityHistoric.txt', 'a+') as fwrite: fread_lines = fread.readlines() for i in fread_lines: fwrite.write(i) 

What happens here is, every time I run the script, it writes the current run number say HistoricData.txt contains 1 2 3 4 and DailyData.txt contains 5. When I run my script, DailyData.txt will contain 6, it instead of copying the 6, script copies 5. If I run it again, it will copy 6 and not 7.

My script is like,

class MyClass: stdout = open('historicData.txt', 'a+') try: # my code Selenium stuff except: # my code finally: # copy data to HistoricData here 

What I am trying to achieve here is script should copy the same data from DailyData in HistoricData

Redirect the data in 2 different files at the same time.

Any help will be appreciated.

2 Answers 2

1

I encourage you to use the logging module https://docs.python.org/2/howto/logging-cookbook.html

You can create two appenders (one per file) and a logging format that suits your needs.

Monkey patching sys.stdout is a hacky way to achieve what you can do with the proper classes already included in the standard library.

Sign up to request clarification or add additional context in comments.

Comments

0

If I am understanding this correctly you are trying to write some data to a file(Dailydata.txt), copy it on another(historicData.txt) then at the second iteration you would like Dailydata.txt to be rewritten with the new data and add the new data at the and of historicData.txt.

You can try this:

with open('Dailydata.txt', 'rb') as r: with open('historicData.txt', 'ab') as w: lines = r.readlines() for line in lines: w.write(str(line)) 

If you, instead, want to write the same data at the same time in both files you just have to explicitly do so:

def my_data_generator(): #do stuff def my_writer(): daily = open('Dailydata.txt', 'w') historic = open('historicData.txt', 'ab') data = my_data_generator() daily.write(data) historic.write(data) 

2 Comments

Thanks for your answer. What I want is redirect the data into 2 different files. I can't stdout data in 2 different files at the same time. So, what I am doing is, I redirect data in DailyData first then write it into HistoricData. Now things get tricky here. Lets say I ran the script for the first time and outputted 1 in in DailyData but my write code(as above) does not copy 1 into HistoricData. When I ran the script 2nd time, it writes 2 in DailyData and 1 in HistoricData. If I run it 3rd time, it will copy 3 in DD and 2 in HD and so on. HD shld hv same data as DD
Ok, I edited my answer, instead of saving the data directly on file you should store it in a variable then write the data in both files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.