Having troubles when creating a txt.file for everytime I run my python code. The code is supposed to collect data for quite some time, but whenever I name the txt.file by the current time in hours and minutes (ex. 12:00), the script will create a new file whenever the clock hits the next minute (ex. 12:01) while the code is running. Anyway of stopping this from happening?
Code:
import sys
sys.path.append('../../')
import time
import datetime
from DFRobot_BMX160 import BMX160
bmx = BMX160(1)
while not bmx.begin():
time.sleep(2)
def main():
while True:
data= bmx.get_all_data()
moment = time.strftime("%d-%m-%Y",time.localtime())
file = open('IMU ' + moment + '.txt', "a")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")[:-3] + " ")
file.write("magnetometer {0:.2f} uT {1:.2f} uT {2:.2f} uT".format(data[0],data[1],data[2]) + "\n")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")[:-3] + " ")
file.write("gyroscope {0:.2f} g {1:.2f} g {2:.2f} g".format(data[3],data[4],data[5]) + "\n")
file.write(datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S.%f")[:-3] + " ")
file.write("accelerometer {0:.2f} m/s^2 {1:.2f} m/s^2 {2:.2f} m/s^2".format(data[6],data[7],data[8]) + "\n")
if __name__ == "__main__":
main()