Problem in creating multiple json files in python for loop

asp

I am trying to create multiple json files in for loop, I am trying below, but i am getting error below

OSError: [Errno 22] Invalid argument: 'c:\\csv\x0bolume_current_{vol_size}.json'

What I tried:

storage_info =({"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name})
for s in storage_info:
      filename = 'c:\csv\volume_current_{vol_size}.json'
      with open(filename, "w") as f:
         json.dump(storage_info, f, indent=4, separators=(',', ': '))

as i get multiple volume size values for example 8, 2 in main loop i need to create a json for each size as one file..like volumen_current_8.json volume_current_2.json like that... anyhelp appreciated.

ParthS007

You are not mentioning the right key for vol_size.

I am assuming the storage_info as list of multiple dict.

You can do like this:

storage_info = [{"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name}, {"VolumeType": vol_type_one,"Size": vol_size_one,"InstanceId": inst_id_one,"Encrypted": encryption_status_one,"AliasName": alias_name_one}]

for s in storage_info:
      filename = 'c:\csv\\volume_current_{}.json'.format(s['Size'])
      with open(filename, "w") as f:
         json.dump(storage_info, f, indent=4, separators=(',', ': '))

If your storage_info is dict itself, then you have to do like this for every storage_info you get:


storage_info = {"VolumeType": vol_type,"Size": vol_size,"InstanceId": inst_id,"Encrypted": encryption_status,"AliasName": alias_name}

filename = 'c:\csv\\volume_current_{}.json'.format(storage_info['Size'])
with open(filename, "w") as f:
    json.dump(storage_info, f, indent=4, separators=(',', ': '))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

creating multiple csv files from php loop

From Dev

Python: Checking for JSON files and creating one if needed

From Dev

Python: Loop to open multiple folders and files in python

From Dev

Creating multiple lists in for loop with dynamic names in Python

From Dev

Creating Multiple .txt files from an Excel file with Python Loop

From Dev

Creating multiple files for for-loop outputs

From Dev

How can I open multiple json files in Python with a for loop?

From Dev

creating multiple user named and numbered files without loop from bash

From Dev

Creating multiple dataframes in loop

From Dev

Problem creating JSON data in asynchronous loop with Vuejs and FileReader

From Dev

Creating multiple class objects with a for loop in python

From Dev

Creating multiple variables in a loop

From Dev

Creating multiple matrices with a for loop

From Dev

Creating multiple objects in loop

From Dev

creating multiple python class instance from json

From Dev

Creating multiple files in Python 3.xx

From Dev

Creating multiple dataframes with a loop

From Dev

Python for loop through array creating JSON file

From Dev

Creating cache files with loop

From Dev

creating loop in downloading files

From Dev

Creating and saving multiple .xlsx files using for loop openxlsx

From Dev

Creating JSON from multiple dataframes python

From Dev

Creating multiple scatterplots with a for loop

From Dev

Creating Multiple output files using pandas in python

From Dev

Python logging is not creating multiple instance of log files

From Dev

creating multiple columns in a for loop python

From Dev

loop through multiple json files as inputs in Terraform

From Dev

Trouble creating for loop with multiple commands to read csv files

From Dev

open json files in a loop - formatting problem

Related Related

  1. 1

    creating multiple csv files from php loop

  2. 2

    Python: Checking for JSON files and creating one if needed

  3. 3

    Python: Loop to open multiple folders and files in python

  4. 4

    Creating multiple lists in for loop with dynamic names in Python

  5. 5

    Creating Multiple .txt files from an Excel file with Python Loop

  6. 6

    Creating multiple files for for-loop outputs

  7. 7

    How can I open multiple json files in Python with a for loop?

  8. 8

    creating multiple user named and numbered files without loop from bash

  9. 9

    Creating multiple dataframes in loop

  10. 10

    Problem creating JSON data in asynchronous loop with Vuejs and FileReader

  11. 11

    Creating multiple class objects with a for loop in python

  12. 12

    Creating multiple variables in a loop

  13. 13

    Creating multiple matrices with a for loop

  14. 14

    Creating multiple objects in loop

  15. 15

    creating multiple python class instance from json

  16. 16

    Creating multiple files in Python 3.xx

  17. 17

    Creating multiple dataframes with a loop

  18. 18

    Python for loop through array creating JSON file

  19. 19

    Creating cache files with loop

  20. 20

    creating loop in downloading files

  21. 21

    Creating and saving multiple .xlsx files using for loop openxlsx

  22. 22

    Creating JSON from multiple dataframes python

  23. 23

    Creating multiple scatterplots with a for loop

  24. 24

    Creating Multiple output files using pandas in python

  25. 25

    Python logging is not creating multiple instance of log files

  26. 26

    creating multiple columns in a for loop python

  27. 27

    loop through multiple json files as inputs in Terraform

  28. 28

    Trouble creating for loop with multiple commands to read csv files

  29. 29

    open json files in a loop - formatting problem

HotTag

Archive