Python - Write multiple JSON files each time user submits the form












-1














This code writes a file named vip.json. Currently, it is overwriting the same file each time i submit the form. But, I want - Each time i click on submit in my form (which is built in flask) I want new files created for each submission. Something like - vip1.json, vip2.json, vip3.json and so on each time the form is submitted.



from flask import Flask, render_template, url_for, flash, redirect, request, 
jsonify, json
from forms import RequestForm

@app.route("/home", methods=['POST'])
def home():
form = RequestForm()
employee_id = form.employee_id.data
email = form.email.data
network = form.network.data
app_name = form.app_name.data
vip_name = form.vip_name.data
pool_name = form.pool_name.data
pool_monitor = form.pool_monitor.data
pool_member = form.pool_member.data
load_balance = form.load_balance.data
ssl = form.ssl.data

data={}

data = {
'Employee ID': employee_id,
'Email': email,
'Network': network,
'App Name': app_name,
'VIP Name': vip_name,
'Pool name': pool_name,
'Pool Monitor': pool_monitor,
'Pool Member': pool_member,
'Load Balancing Method': load_balance,
'SSL': ssl
}

if form.validate_on_submit():
with open("C:\pytest\vip.json",'w') as j:
json.dump(data, j)

return render_template ('home.html', title='Home', data=data, form=form, employee_id=employee_id, email=email, network=network, app_name=app_name, vip_name=vip_name, pool_name=pool_name, pool_monitor=pool_monitor, pool_member=pool_member, load_balance=load_balance, ssl=ssl)
else:
return render_template('request.html', form=form)


I had a look online but i could not get anything useful. What will be the best way to do this?










share|improve this question





























    -1














    This code writes a file named vip.json. Currently, it is overwriting the same file each time i submit the form. But, I want - Each time i click on submit in my form (which is built in flask) I want new files created for each submission. Something like - vip1.json, vip2.json, vip3.json and so on each time the form is submitted.



    from flask import Flask, render_template, url_for, flash, redirect, request, 
    jsonify, json
    from forms import RequestForm

    @app.route("/home", methods=['POST'])
    def home():
    form = RequestForm()
    employee_id = form.employee_id.data
    email = form.email.data
    network = form.network.data
    app_name = form.app_name.data
    vip_name = form.vip_name.data
    pool_name = form.pool_name.data
    pool_monitor = form.pool_monitor.data
    pool_member = form.pool_member.data
    load_balance = form.load_balance.data
    ssl = form.ssl.data

    data={}

    data = {
    'Employee ID': employee_id,
    'Email': email,
    'Network': network,
    'App Name': app_name,
    'VIP Name': vip_name,
    'Pool name': pool_name,
    'Pool Monitor': pool_monitor,
    'Pool Member': pool_member,
    'Load Balancing Method': load_balance,
    'SSL': ssl
    }

    if form.validate_on_submit():
    with open("C:\pytest\vip.json",'w') as j:
    json.dump(data, j)

    return render_template ('home.html', title='Home', data=data, form=form, employee_id=employee_id, email=email, network=network, app_name=app_name, vip_name=vip_name, pool_name=pool_name, pool_monitor=pool_monitor, pool_member=pool_member, load_balance=load_balance, ssl=ssl)
    else:
    return render_template('request.html', form=form)


    I had a look online but i could not get anything useful. What will be the best way to do this?










    share|improve this question



























      -1












      -1








      -1







      This code writes a file named vip.json. Currently, it is overwriting the same file each time i submit the form. But, I want - Each time i click on submit in my form (which is built in flask) I want new files created for each submission. Something like - vip1.json, vip2.json, vip3.json and so on each time the form is submitted.



      from flask import Flask, render_template, url_for, flash, redirect, request, 
      jsonify, json
      from forms import RequestForm

      @app.route("/home", methods=['POST'])
      def home():
      form = RequestForm()
      employee_id = form.employee_id.data
      email = form.email.data
      network = form.network.data
      app_name = form.app_name.data
      vip_name = form.vip_name.data
      pool_name = form.pool_name.data
      pool_monitor = form.pool_monitor.data
      pool_member = form.pool_member.data
      load_balance = form.load_balance.data
      ssl = form.ssl.data

      data={}

      data = {
      'Employee ID': employee_id,
      'Email': email,
      'Network': network,
      'App Name': app_name,
      'VIP Name': vip_name,
      'Pool name': pool_name,
      'Pool Monitor': pool_monitor,
      'Pool Member': pool_member,
      'Load Balancing Method': load_balance,
      'SSL': ssl
      }

      if form.validate_on_submit():
      with open("C:\pytest\vip.json",'w') as j:
      json.dump(data, j)

      return render_template ('home.html', title='Home', data=data, form=form, employee_id=employee_id, email=email, network=network, app_name=app_name, vip_name=vip_name, pool_name=pool_name, pool_monitor=pool_monitor, pool_member=pool_member, load_balance=load_balance, ssl=ssl)
      else:
      return render_template('request.html', form=form)


      I had a look online but i could not get anything useful. What will be the best way to do this?










      share|improve this question















      This code writes a file named vip.json. Currently, it is overwriting the same file each time i submit the form. But, I want - Each time i click on submit in my form (which is built in flask) I want new files created for each submission. Something like - vip1.json, vip2.json, vip3.json and so on each time the form is submitted.



      from flask import Flask, render_template, url_for, flash, redirect, request, 
      jsonify, json
      from forms import RequestForm

      @app.route("/home", methods=['POST'])
      def home():
      form = RequestForm()
      employee_id = form.employee_id.data
      email = form.email.data
      network = form.network.data
      app_name = form.app_name.data
      vip_name = form.vip_name.data
      pool_name = form.pool_name.data
      pool_monitor = form.pool_monitor.data
      pool_member = form.pool_member.data
      load_balance = form.load_balance.data
      ssl = form.ssl.data

      data={}

      data = {
      'Employee ID': employee_id,
      'Email': email,
      'Network': network,
      'App Name': app_name,
      'VIP Name': vip_name,
      'Pool name': pool_name,
      'Pool Monitor': pool_monitor,
      'Pool Member': pool_member,
      'Load Balancing Method': load_balance,
      'SSL': ssl
      }

      if form.validate_on_submit():
      with open("C:\pytest\vip.json",'w') as j:
      json.dump(data, j)

      return render_template ('home.html', title='Home', data=data, form=form, employee_id=employee_id, email=email, network=network, app_name=app_name, vip_name=vip_name, pool_name=pool_name, pool_monitor=pool_monitor, pool_member=pool_member, load_balance=load_balance, ssl=ssl)
      else:
      return render_template('request.html', form=form)


      I had a look online but i could not get anything useful. What will be the best way to do this?







      python python-3.x flask-wtforms wtforms wtforms-json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 '18 at 4:08

























      asked Nov 12 '18 at 4:00









      Bharath

      306




      306
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You could use glob to scan your directory and get a list of all your json files, get the file with the latest version, then iterate it by one for the new file's name:



          import os
          import glob

          # Use glob to get a list of existing vip files.
          dir = "C:/pytest/"
          files = glob.glob(os.path.join(dir, "vip*.json")) # Let's say it returns ["C:/pytest/vip1.json", "C:/pytest/vip2.json", "C:/pytest/vip3.json"]

          # Grab the latest vip file.
          latest_file = sorted(files)[-1]

          # Strip the path so it's just the file's name.
          file_name = os.path.splitext(os.path.basename(latest_file))[0]

          # Extract the number from the file's name.
          num = int(file_name.lstrip("vip"))

          # Generate your new path.
          new_path = os.path.join(dir, "vip{}.json".format(num + 1))
          # Output of new_path: C:/pytest/vip4.json


          You may need additional error checking, like if num is really a number or if there are no existing files then to default num to 1, but I'll leave that to you.






          share|improve this answer























          • Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
            – Bharath
            Nov 12 '18 at 4:51












          • Go get 'em, code warrior! :D
            – Green Cell
            Nov 12 '18 at 4:55



















          1














          This may not be the best way to do it, but you can append a UUID (Universally Unique IDentifier) if you do this:



          import uuid
          if form.validate_on_submit():
          filename "vip-"+str(uuid.uuid4())+".json"
          with open("C:\pytest\"+filename,'w') as j:
          json.dump(data, j)


          The probability of clashing is very low, but you can always check if the file exists and generate another one if it does.



          If you want to serialize, you can do it by:




          • Storing a pickle that has your filecount

          • Storing the current count in a database


            • I do not know anything about flask or the ORM you are using (if you are), so I'll leave that up to you



          • Using the information in this SO post to get a list of the files and add add 1 to len(list of files) to get your count (this assumes that only these files exist in the directory)

          • Use the same SO post to fetch the list of files, use RegEx to filter out files matching your particular pattern, then add 1 to the highest


          To use the pickle approach, go to the directory where your python file is, and run this once:



          import pickle
          counter=1;
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)


          This will store a vip_counter.pickle in your file system, where the script is run make sure that the pickle file is in the right spot



          Every time before you exit, you need to update the file in the same fashion:



          with open("vip_counter.pickle", "rb")as p:
          counter=pickle.load()
          #counter is now loaded
          counter+=1 #increment your counter before the new filesave
          #your code here

          #save your pickle back again :)
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)





          share|improve this answer























          • Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
            – Bharath
            Nov 12 '18 at 4:20










          • No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
            – robotHamster
            Nov 12 '18 at 4:25










          • thanks for taking the time! I'll try this now :)
            – Bharath
            Nov 12 '18 at 4:32










          • No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
            – robotHamster
            Nov 12 '18 at 4:38










          • I was also thinking uuid would be an easy approach! +1
            – Green Cell
            Nov 12 '18 at 4:43













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255801%2fpython-write-multiple-json-files-each-time-user-submits-the-form%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You could use glob to scan your directory and get a list of all your json files, get the file with the latest version, then iterate it by one for the new file's name:



          import os
          import glob

          # Use glob to get a list of existing vip files.
          dir = "C:/pytest/"
          files = glob.glob(os.path.join(dir, "vip*.json")) # Let's say it returns ["C:/pytest/vip1.json", "C:/pytest/vip2.json", "C:/pytest/vip3.json"]

          # Grab the latest vip file.
          latest_file = sorted(files)[-1]

          # Strip the path so it's just the file's name.
          file_name = os.path.splitext(os.path.basename(latest_file))[0]

          # Extract the number from the file's name.
          num = int(file_name.lstrip("vip"))

          # Generate your new path.
          new_path = os.path.join(dir, "vip{}.json".format(num + 1))
          # Output of new_path: C:/pytest/vip4.json


          You may need additional error checking, like if num is really a number or if there are no existing files then to default num to 1, but I'll leave that to you.






          share|improve this answer























          • Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
            – Bharath
            Nov 12 '18 at 4:51












          • Go get 'em, code warrior! :D
            – Green Cell
            Nov 12 '18 at 4:55
















          1














          You could use glob to scan your directory and get a list of all your json files, get the file with the latest version, then iterate it by one for the new file's name:



          import os
          import glob

          # Use glob to get a list of existing vip files.
          dir = "C:/pytest/"
          files = glob.glob(os.path.join(dir, "vip*.json")) # Let's say it returns ["C:/pytest/vip1.json", "C:/pytest/vip2.json", "C:/pytest/vip3.json"]

          # Grab the latest vip file.
          latest_file = sorted(files)[-1]

          # Strip the path so it's just the file's name.
          file_name = os.path.splitext(os.path.basename(latest_file))[0]

          # Extract the number from the file's name.
          num = int(file_name.lstrip("vip"))

          # Generate your new path.
          new_path = os.path.join(dir, "vip{}.json".format(num + 1))
          # Output of new_path: C:/pytest/vip4.json


          You may need additional error checking, like if num is really a number or if there are no existing files then to default num to 1, but I'll leave that to you.






          share|improve this answer























          • Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
            – Bharath
            Nov 12 '18 at 4:51












          • Go get 'em, code warrior! :D
            – Green Cell
            Nov 12 '18 at 4:55














          1












          1








          1






          You could use glob to scan your directory and get a list of all your json files, get the file with the latest version, then iterate it by one for the new file's name:



          import os
          import glob

          # Use glob to get a list of existing vip files.
          dir = "C:/pytest/"
          files = glob.glob(os.path.join(dir, "vip*.json")) # Let's say it returns ["C:/pytest/vip1.json", "C:/pytest/vip2.json", "C:/pytest/vip3.json"]

          # Grab the latest vip file.
          latest_file = sorted(files)[-1]

          # Strip the path so it's just the file's name.
          file_name = os.path.splitext(os.path.basename(latest_file))[0]

          # Extract the number from the file's name.
          num = int(file_name.lstrip("vip"))

          # Generate your new path.
          new_path = os.path.join(dir, "vip{}.json".format(num + 1))
          # Output of new_path: C:/pytest/vip4.json


          You may need additional error checking, like if num is really a number or if there are no existing files then to default num to 1, but I'll leave that to you.






          share|improve this answer














          You could use glob to scan your directory and get a list of all your json files, get the file with the latest version, then iterate it by one for the new file's name:



          import os
          import glob

          # Use glob to get a list of existing vip files.
          dir = "C:/pytest/"
          files = glob.glob(os.path.join(dir, "vip*.json")) # Let's say it returns ["C:/pytest/vip1.json", "C:/pytest/vip2.json", "C:/pytest/vip3.json"]

          # Grab the latest vip file.
          latest_file = sorted(files)[-1]

          # Strip the path so it's just the file's name.
          file_name = os.path.splitext(os.path.basename(latest_file))[0]

          # Extract the number from the file's name.
          num = int(file_name.lstrip("vip"))

          # Generate your new path.
          new_path = os.path.join(dir, "vip{}.json".format(num + 1))
          # Output of new_path: C:/pytest/vip4.json


          You may need additional error checking, like if num is really a number or if there are no existing files then to default num to 1, but I'll leave that to you.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 4:37

























          answered Nov 12 '18 at 4:32









          Green Cell

          1,866825




          1,866825












          • Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
            – Bharath
            Nov 12 '18 at 4:51












          • Go get 'em, code warrior! :D
            – Green Cell
            Nov 12 '18 at 4:55


















          • Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
            – Bharath
            Nov 12 '18 at 4:51












          • Go get 'em, code warrior! :D
            – Green Cell
            Nov 12 '18 at 4:55
















          Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
          – Bharath
          Nov 12 '18 at 4:51






          Beautiful piece of explanation. Thank You very much! Works exactly how i wanted :)
          – Bharath
          Nov 12 '18 at 4:51














          Go get 'em, code warrior! :D
          – Green Cell
          Nov 12 '18 at 4:55




          Go get 'em, code warrior! :D
          – Green Cell
          Nov 12 '18 at 4:55













          1














          This may not be the best way to do it, but you can append a UUID (Universally Unique IDentifier) if you do this:



          import uuid
          if form.validate_on_submit():
          filename "vip-"+str(uuid.uuid4())+".json"
          with open("C:\pytest\"+filename,'w') as j:
          json.dump(data, j)


          The probability of clashing is very low, but you can always check if the file exists and generate another one if it does.



          If you want to serialize, you can do it by:




          • Storing a pickle that has your filecount

          • Storing the current count in a database


            • I do not know anything about flask or the ORM you are using (if you are), so I'll leave that up to you



          • Using the information in this SO post to get a list of the files and add add 1 to len(list of files) to get your count (this assumes that only these files exist in the directory)

          • Use the same SO post to fetch the list of files, use RegEx to filter out files matching your particular pattern, then add 1 to the highest


          To use the pickle approach, go to the directory where your python file is, and run this once:



          import pickle
          counter=1;
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)


          This will store a vip_counter.pickle in your file system, where the script is run make sure that the pickle file is in the right spot



          Every time before you exit, you need to update the file in the same fashion:



          with open("vip_counter.pickle", "rb")as p:
          counter=pickle.load()
          #counter is now loaded
          counter+=1 #increment your counter before the new filesave
          #your code here

          #save your pickle back again :)
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)





          share|improve this answer























          • Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
            – Bharath
            Nov 12 '18 at 4:20










          • No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
            – robotHamster
            Nov 12 '18 at 4:25










          • thanks for taking the time! I'll try this now :)
            – Bharath
            Nov 12 '18 at 4:32










          • No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
            – robotHamster
            Nov 12 '18 at 4:38










          • I was also thinking uuid would be an easy approach! +1
            – Green Cell
            Nov 12 '18 at 4:43


















          1














          This may not be the best way to do it, but you can append a UUID (Universally Unique IDentifier) if you do this:



          import uuid
          if form.validate_on_submit():
          filename "vip-"+str(uuid.uuid4())+".json"
          with open("C:\pytest\"+filename,'w') as j:
          json.dump(data, j)


          The probability of clashing is very low, but you can always check if the file exists and generate another one if it does.



          If you want to serialize, you can do it by:




          • Storing a pickle that has your filecount

          • Storing the current count in a database


            • I do not know anything about flask or the ORM you are using (if you are), so I'll leave that up to you



          • Using the information in this SO post to get a list of the files and add add 1 to len(list of files) to get your count (this assumes that only these files exist in the directory)

          • Use the same SO post to fetch the list of files, use RegEx to filter out files matching your particular pattern, then add 1 to the highest


          To use the pickle approach, go to the directory where your python file is, and run this once:



          import pickle
          counter=1;
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)


          This will store a vip_counter.pickle in your file system, where the script is run make sure that the pickle file is in the right spot



          Every time before you exit, you need to update the file in the same fashion:



          with open("vip_counter.pickle", "rb")as p:
          counter=pickle.load()
          #counter is now loaded
          counter+=1 #increment your counter before the new filesave
          #your code here

          #save your pickle back again :)
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)





          share|improve this answer























          • Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
            – Bharath
            Nov 12 '18 at 4:20










          • No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
            – robotHamster
            Nov 12 '18 at 4:25










          • thanks for taking the time! I'll try this now :)
            – Bharath
            Nov 12 '18 at 4:32










          • No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
            – robotHamster
            Nov 12 '18 at 4:38










          • I was also thinking uuid would be an easy approach! +1
            – Green Cell
            Nov 12 '18 at 4:43
















          1












          1








          1






          This may not be the best way to do it, but you can append a UUID (Universally Unique IDentifier) if you do this:



          import uuid
          if form.validate_on_submit():
          filename "vip-"+str(uuid.uuid4())+".json"
          with open("C:\pytest\"+filename,'w') as j:
          json.dump(data, j)


          The probability of clashing is very low, but you can always check if the file exists and generate another one if it does.



          If you want to serialize, you can do it by:




          • Storing a pickle that has your filecount

          • Storing the current count in a database


            • I do not know anything about flask or the ORM you are using (if you are), so I'll leave that up to you



          • Using the information in this SO post to get a list of the files and add add 1 to len(list of files) to get your count (this assumes that only these files exist in the directory)

          • Use the same SO post to fetch the list of files, use RegEx to filter out files matching your particular pattern, then add 1 to the highest


          To use the pickle approach, go to the directory where your python file is, and run this once:



          import pickle
          counter=1;
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)


          This will store a vip_counter.pickle in your file system, where the script is run make sure that the pickle file is in the right spot



          Every time before you exit, you need to update the file in the same fashion:



          with open("vip_counter.pickle", "rb")as p:
          counter=pickle.load()
          #counter is now loaded
          counter+=1 #increment your counter before the new filesave
          #your code here

          #save your pickle back again :)
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)





          share|improve this answer














          This may not be the best way to do it, but you can append a UUID (Universally Unique IDentifier) if you do this:



          import uuid
          if form.validate_on_submit():
          filename "vip-"+str(uuid.uuid4())+".json"
          with open("C:\pytest\"+filename,'w') as j:
          json.dump(data, j)


          The probability of clashing is very low, but you can always check if the file exists and generate another one if it does.



          If you want to serialize, you can do it by:




          • Storing a pickle that has your filecount

          • Storing the current count in a database


            • I do not know anything about flask or the ORM you are using (if you are), so I'll leave that up to you



          • Using the information in this SO post to get a list of the files and add add 1 to len(list of files) to get your count (this assumes that only these files exist in the directory)

          • Use the same SO post to fetch the list of files, use RegEx to filter out files matching your particular pattern, then add 1 to the highest


          To use the pickle approach, go to the directory where your python file is, and run this once:



          import pickle
          counter=1;
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)


          This will store a vip_counter.pickle in your file system, where the script is run make sure that the pickle file is in the right spot



          Every time before you exit, you need to update the file in the same fashion:



          with open("vip_counter.pickle", "rb")as p:
          counter=pickle.load()
          #counter is now loaded
          counter+=1 #increment your counter before the new filesave
          #your code here

          #save your pickle back again :)
          with open("vip_counter.pickle", "wb") as p:
          pickle.dump(p, counter)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 '18 at 4:37

























          answered Nov 12 '18 at 4:09









          robotHamster

          343115




          343115












          • Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
            – Bharath
            Nov 12 '18 at 4:20










          • No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
            – robotHamster
            Nov 12 '18 at 4:25










          • thanks for taking the time! I'll try this now :)
            – Bharath
            Nov 12 '18 at 4:32










          • No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
            – robotHamster
            Nov 12 '18 at 4:38










          • I was also thinking uuid would be an easy approach! +1
            – Green Cell
            Nov 12 '18 at 4:43




















          • Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
            – Bharath
            Nov 12 '18 at 4:20










          • No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
            – robotHamster
            Nov 12 '18 at 4:25










          • thanks for taking the time! I'll try this now :)
            – Bharath
            Nov 12 '18 at 4:32










          • No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
            – robotHamster
            Nov 12 '18 at 4:38










          • I was also thinking uuid would be an easy approach! +1
            – Green Cell
            Nov 12 '18 at 4:43


















          Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
          – Bharath
          Nov 12 '18 at 4:20




          Thanks @robotHamster. Works like a charm. Fixes for the time being. But i'm looking something like human readable, like vip1, vip2, etc. Many thanks for your answer! :) If you could modify this more i would be more than happy, sorry i'm a rookie to python.
          – Bharath
          Nov 12 '18 at 4:20












          No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
          – robotHamster
          Nov 12 '18 at 4:25




          No worries, I think the simplest without knowing what your whole stack looks like is a pickle. I will edit it to include the pickle. Brewing some code....
          – robotHamster
          Nov 12 '18 at 4:25












          thanks for taking the time! I'll try this now :)
          – Bharath
          Nov 12 '18 at 4:32




          thanks for taking the time! I'll try this now :)
          – Bharath
          Nov 12 '18 at 4:32












          No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
          – robotHamster
          Nov 12 '18 at 4:38




          No worries :) The other answer posted by @GreenCell also works. If you chose his approach, I wouldn't mind an upvote :)
          – robotHamster
          Nov 12 '18 at 4:38












          I was also thinking uuid would be an easy approach! +1
          – Green Cell
          Nov 12 '18 at 4:43






          I was also thinking uuid would be an easy approach! +1
          – Green Cell
          Nov 12 '18 at 4:43




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255801%2fpython-write-multiple-json-files-each-time-user-submits-the-form%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation