Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create filename with metadata

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.

like image 984
Ann Avatar asked Aug 09 '26 18:08

Ann


2 Answers

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)
like image 149
OneCricketeer Avatar answered Aug 11 '26 08:08

OneCricketeer


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.

like image 22
Ann Avatar answered Aug 11 '26 09:08

Ann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!