0

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.

0

3 Answers 3

1

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=(',', ': '))
Sign up to request clarification or add additional context in comments.

9 Comments

I am getting filename = 'c:\csv\volume_current_{}.json'.format(s['Size']) TypeError: string indices must be integers error
each time in loop i get that storage_info like this `{'VolumeType': 'gp', 'Size': 2, 'InstanceId': 'xxxxxxxxxxx', 'Encrypted': True, 'AliasName': 'EBS'}
s is {'VolumeType': 'gp', 'Size': 2, 'InstanceId': 'xxxxxxxxxxx', 'Encrypted': True, 'AliasName': 'EBS'}. Right?
yes. each iteration will get all volumes one by one like above
tried, getting same OSError: [Errno 22] Invalid argument: 'c:\\csv\x0bolume_current_8.json' . huh...it seems its not able to form path ..dont know why :( i am running it in windows
|
1

The problem is with filename variable. The variable is static to a string. You can simply use f-string literels to solve the problem.

Just change filename = 'c:\csv\volume_current_{vol_size}.json' to filename = f'c:\csv\volume_current_{vol_size}.json'

and everything should work fine.

Complete code

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

5 Comments

tried getting same error..its not substituing file path properly :(
c:\csvolume_current_8.json when i tried to print file name it seems it has problem with \ in path
What is the value of the variable vol_size? Have you declared it or are you getting it from storage_info? Because if you are getting it from the dictionaries stored in the variable storage_info, filename = f'c:\csv\volume_current_{s['vol_size']}.json' should work.
How did your file name get changed from 'c:\\csv\x0bolume_current_{vol_size}.json' to just c:\csvolume_current_8.json. Expected behaviour was 'c:\\csv\volume_current_8.json'
an additional \ in path fixed the issue filename = f'c:\csv\\volume_current_{vol_size}.json'
1

I fixed the issue now, all i did add an extra \ in below path. Not sure whether correct way or not it now consider path as correct

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

Edit2:

replaced \ to / in path it works (some learning that i should use /)

filename = f'c:\csv\\volume_current_{vol_size}.json' 
to   
filename = f'c:/csv/volume_current_{vol_size}.json'

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.