I have to create several undefined files when the Internet is disconnected and await a json inside each of those files, but I do not create several files I do not know how to create file_1, then file_2, so consecutively since it does not I know how many files will be created, this is my code:
import subprocess, time
import os
import errno
import json
import urllib2
import socket
import requests
from datetime import datetime
from os import listdir
hostname = "www.google.com"
rootDir = './Json'
nombre = datetime.utcnow().strftime('%Y-%m-%d %H_%M_%S.%f')[:-3]
filename = "Json/%s.json"% nombre
#"Json/%s.json"% time.strftime("%Y-%m-%d %H:%M:%S")
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(.300)
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("Da ighual")