Using Python to Select and Load a Random File
I had a task to create a mechanism to load sample data so that we could do some demonstrations. Our incoming data is in JSON format so I took a bunch of requests and put them into individual text files. The next task was to create a little Python program to choose one of the files randomly, take the JSON and send it into our system.
import random
import glob
filename = random.choice(glob.glob('./*.json'))
Using glob, I get a list of the filenames that match the mask of “*.json”. The choice() function is a handy little tool. It chooses and returns a random element from the list. That gives us a random file from out of the working directory.
In the case of this task I needed to load read the JSON, make some minor changes, and then send into the system, using a cURL command. So, next we read from the selected file and make a little example modification:
with open(filename, 'r') as f: json_str = f.read() j = json.loads(json_str) j['customer_name'] = 'Test Customer'
Next, I wanted to write the modified JSON to a unique file so that I could pass that into cURL. I had considered using a TemporaryFile, but I wanted to be able to close the file and make sure it’s written to disk without deleting the file first (because I need to pass it to cURL).
unique_filename = str(uuid.uuid4()) with open(unique_filename, "w") as f: f.write(json.dumps(j, indent=4))
Next, we’re ready to post the file and clean up our little mess of a temporary file.
server = 'URL of your server to which you post JSON'
cmd = "curl -X POST -H 'Content-Type: application/json' -d '@{0}' {1}".format(unique_filename, server)
print('Command: {0}'.format(cmd))
os.remove(unique_filename)
Pretty straightforward overall. Here’s the whole source file. Have at it!
