How to create a file in Filemanger in Code

Good day,
Am trying to create a file in file manager that is generated by code.

f = open("/files/autoname.xml", "wb")
f.write("file contente")

But the above code gives me an error.
FileNotFoundError: [Errno 2] No such file or directory: '/files/autoname.xml'

Something like this should work. Your goal is to create an instance of the 'File' DocType, and use it.

file_doc = frappe.get_doc(
	"doctype": "File",
	"file_name": some_file_name,  # String that is your file's name
	"attached_to_doctype": None,
	"attached_to_name": None,
	"content": your_content,  # Your text/data content goes here.
	"folder": "Home"  #  If you want to save in subdirectory, enter 'Home/MySubdirectory'

Now if you open the “Files” DocType in your browser, you should see a new file with your content.


Thank you, this worked, you are a life saver