Can we create a Doctype via Code not via UI?

Is it possible to create the new doctype using code . Initially we create the new doctype via UI but i have to create it via code .

Yeah its possible

How ? can you please tell me?

Create a doctype in actual DocType

new_doc = frappe.new_doc("DocType")

I have to create actual doctype via code. Can it is posible?

doc_type_properties = {
“doctype”: “doctype name”,
“name_field”: “name”,
“fields”: [
“label”: " Field Name",
“fieldname”: “field_name”,
“fieldtype”: “Data”,
“reqd”: 1
“label”: “Second Field Name”,
“fieldname”: “second_field_name”,
“fieldtype”: “Data”
“permissions”: [
“role”: “System Manager”,
“read”: 1,
“write”: 1

doc_type = get_doc({
“doctype”: “doctype name”,
“module”: “Name”,
“custom”: 1,
“name”: doc_type_properties[“doctype”],
“fields”: doc_type_properties[“fields”],
“permissions”: doc_type_properties[“permissions”],
“name_field”: doc_type_properties[“name_field”]


Inside which folder or directory we have to make these changes or we have to make the new file , if yes then inside which dir.

In which file this code should be written. Or in which folder or dir we have to create the file to wrote this code.