If it possible to create md file with some atributes and save it to .md format on python? So i have atributes:
year="2018"
month="12"
and i need save it, for example, to 2018_12_metadata.md file with needful atributes. Here's how md file should look like
{
"year": "2018",
"month": "12"
}
So i have some code, but i could find how save it to md format with needful atributes:
year="2018"
month="12"
path_folder="C:\\Users\"
MD_output = path_folder + year +'-'+ month +'_metadata.md'
md_file - what should be here?
md_file.write(MD_output)
Many thanks for any help.
Make a dictionary
metadata = {
"year": "2018",
"month": "12"
}
Setup your filepath
path_folder="C:\\Users\\"
md_file = "{}\\{year}_{month}_metadata.md".format(path_folder, *metadata)
Open a file and write to it
with open(md_file, 'w') as f:
f.write("foobar")
Or if all you wanted to do is write a dictionary to a file, load it as JSON
import json
with open(md_file, 'w') as f:
json.dump(metadata, f)
Dears, here is solution that i found:
result = {'year': '2018',
'month': '12'}
file_name="Check"
with open(os.path.join(path_folder, '{}.md'.format(file_name)), mode='w') as md_file:
json.dump(result, md_file, indent=2)
Thanks all for help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With