D
D
duduychvbk992021-10-07 23:39:59
Python
duduychvbk99, 2021-10-07 23:39:59

How to do this in python?

I have 100+ folders so to speak. Each of them contains the events.txt file. I need to do some work in all of them at the same time, for example, change the text in the file to 1 instead of 0. How can this be done through os?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
qpyparty, 2021-10-10
@duduychvbk99

import os
for dir_ in os.listdir():
    os.chdir(dir_)
    f = open("events.txt")
    f_write = open("events.txt", "w")
    f_write.write("0" if int(f.read()) else "1")
    f.close()
    f_write.close()
    os.chdir("..")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question