Q:

Dynamically writing to text file based on received dictionary information

I am trying to write jobs to a text file based on dictionary information. The dictionary information is received from the server and is not always the same. It contains file_count and file_paths which the file_paths is a list. e.g,
{'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2} 

I have a baseline to write out a block of text which contains variables that would be inserted based on the dictionary's information. e.g,
text_baseline = ('This is the %s file\n'
                'and the file path is\n'
                'path: %s\n')

This baseline would need to be duplicated based on the number of files received from the dictionary and written into a text file.
So, for example, if the dictionary has three files it would have three blocks of text each with updated information of file number and paths.
I know that I have to do something like this:
f = open("myfile.txt", "w")
for i in dict.get("file_count"):
    f.write(text_baseline)     # this needs to write in the paths and the file numbers

I am having a hard time figuring out how to update the paths and file numbers based on the info received using the baseline.

A:

use str.format() to format the string.
data = {'file_paths': ['/file/path/one', '/file/path/two'], 'file_count': 2} 
text_baseline = "this is the {}th file and the path is {}"
with open('path','w') as f:
    for i in range(int(dict.get('file_count'))):
         f.write(text_baseline.format(i,data['file_paths']))

