1

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)
5
  • This code doesn't do anything because it never calls get_json. Is there a loop somewhere? Or is this file executed repeatedly? (If so, where's the call to get_json?) In short, this code is okay, and if you call get_json, it should create exactly one file, named after the time when the code was run. Commented Jun 30, 2017 at 5:42
  • As a wild guess, you may want to move those first two lines inside of get_json. (This would fix code that looks like this but then has a loop that calls get_json repeatedly without recalculating filename.) Commented Jun 30, 2017 at 5:43
  • get_json is call in method net_is_up() Commented Jun 30, 2017 at 5:46
  • Your edit seems to confirm my guess, so I've added an answer. Commented Jun 30, 2017 at 5:46
  • Problem is with os.path.dirname(filename) as if this dirname does not exists then it will return empty string and I think there will always be the folder with blank name or some error will surely be there. Commented Jun 30, 2017 at 5:52

2 Answers 2

1

Move these lines inside the get_json function:

name = datetime.utcnow().strftime('%Y-%m-%d %H_%M_%S.%f')[:-3]
filename = "Json/%s.json"% name

As it stands now, filename is only calculated once, when you start this script. You need to do it each time you're going to save a file.

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

Comments

0

This is the answer:

def get_json():
    name= datetime.utcnow().strftime('%Y-%m-%d %H_%M_%S.%f')[:-3]
    filename = "Json/%s.json"% name
    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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.