IOError: [Errno 22] invalid mode ('w+') or filename: 'hodor_2017-05-09_14:03:38.txt'
So I was having issues creating a file where as it is "name" [delimiter] "datetime" .txt
I was looking up different bits of code such as:
Turn a string into a valid filename?
python: how to convert a string to utf-8
https://github.com/django/django/blob/master/django/utils/safestring.py
and it still seems to not work for me.
My concept is simple: given a name and content write a file with that name and that content.
My code is:
def create_json_file(name, contents):
filename = u"%s_%s.json" % (name, datetime.datetime.now().strftime("%Y/%m/%d_%H:%M:%S"))
print "%s" % filename
filename = slugify(filename)
f = open(filename, "w+")
f.write(contents)
f.close()
and as you can see i have been tweaking it. I was looking up the results that django does, which uses slugify.
My original did not have that line. Maybe there is a better way to name the file too. I think the name and datetime is pretty normal but i wasnt sure what delimiters I should be using between name and datetime etc.
For the record, I am not currently using Django because i dont have a need for the framework. I am just trying to test a way to pass in a string and a json map and turn it into a config.json
file essentially.
Eventually, I will want to leverage an AJAX request from a website to do this, but that is outside the scope of this question.