Quantcast
Channel: Active questions tagged visual-studio-code - Stack Overflow
Viewing all articles
Browse latest Browse all 97355

Append data to json file if not in it already

$
0
0

I have a .json file which I would like to append data to. But only if this string is not in the .json file already.

JSON File:

[{"filename":"file1"}, {"filename":"file2"}, {"filename":"file3"}]

End result JSON File:

[{"filename":"file1"}, {"filename":"file2"}, {"filename":"file3"}, {"filename":"file4"}]

I currently have this:

with open('gdrivefiles.json', 'r') as f:
     filenameslist = json.load(f)    #Loads the .json file into a string (If I'm right)
for distro in filenameslist:
     filenames = distro["filename"]  #Gets a list of all the filenames

if name in filenames:
   print("yes")                      #If name is in the list of filenames print 'yes'
else:
   print("no")                       #If name is in the list of filenames print 'no'

(This code is put in a for-loop, so it will run this code for every new value of name)

How would I add name ({"filename":"name"}) in the json file if it is not in it already?


Viewing all articles
Browse latest Browse all 97355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>