I need create multiple files with different names but I recive data each seconds then I need save this data in each file with diferent names but my only code makes a file and when you receive another data this over write the existing file and not create another.
This is my code:
name= datetime.utcnow().strftime('%Y-%m-%d %H_%M_%S.%f')[:-3]
filename = "Json/%s.json"% name
def get_json():
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
with open(filename, "w") as f:
f.write("Hello")
def net_is_up():
while(1):
response = os.system("ping -c 1 " + hostname)
if response == 0:
print "[%s] Network is up!" % time.strftime("%Y-%m-%d %H:%M:%S")
#read_json()
get_json()
else:
print "[%s] Network is down :(" % time.strftime("%Y-%m-%d %H:%M:%S")
time.sleep(60)
get_json. Is there a loop somewhere? Or is this file executed repeatedly? (If so, where's the call toget_json?) In short, this code is okay, and if you callget_json, it should create exactly one file, named after the time when the code was run.get_json. (This would fix code that looks like this but then has a loop that callsget_jsonrepeatedly without recalculatingfilename.)