Editing Newest JSON File In A Directory
June 21, 2019 | 1 Minute Read
This post shows how to edit the newest (by create date) JSON file from a Windows directory.
In this example, we are using Python 3.6.
First, we have to load the needed libraries.
Then, we have to get all JSON files from a directory. Replace the directory here with the directory you want.
Then we add the stats of all the files
S_ISREG checks if the file is a normal file (ex. not a directory). It will return a non-zero value if it is a regular file.
Now we get the latest created JSON file
Last but not least, we open and load the JSON file and add our new key-value pair. After that, we overwrite the existing file.
References:
- https://docs.python.org/3.6/library/stat.html
- https://stackoverflow.com/questions/40163270/what-is-s-isreg-and-what-does-it-do
- https://en.wikipedia.org/wiki/Stat_(system_call)
- https://stackoverflow.com/a/539024