0
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.

Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • 1
    Doesn't this just boil down to Windows disallowing colons in file names? The solution is to use a different separator. – tdelaney May 09 '17 at 20:32

1 Answers1

4

Use a different separator in your filename mask:

filename = u"%s_%s.json" % (name, datetime.datetime.now().strftime("%Y_%m_%d_%H%M%S"))

The OS is trying to open 2005/04/01_5:45:04.json. Slashes aren't allowed in file/directory names.

Edit: Removed colons in response to comments.

Phix
  • 9,364
  • 4
  • 35
  • 62
  • 2
    On Windows, neither are colons. Might think about moving to ISO 8601 basic format -- `%Y%m%dT%H%M%S`, no colons, no slashes, `T` separating the date portion and the time portion... and, well, it's an actual standard. – Charles Duffy May 09 '17 at 20:53
  • Thanks to both of you. I knew about slashes, for some reason i was stupid and wasnt thinking the colon. my bad. Yeah, I will likely just use isoformat() – Fallenreaper May 09 '17 at 22:18
  • @CharlesDuffy just a follow up, doing it in iso format returns this error `IOError: [Errno 22] invalid mode ('w+') or filename: u'hodor_2017-05-10T10:23:07.057000. json'` So it seems it wont do standard ISO, so i moved it to your format. It seems the ISO 8601 used by default with Python's string class is: `YYYY-MM-DDTHH:MM:SS.mmmmmm`. Doing it with the specific format you defined works though. – Fallenreaper May 10 '17 at 14:26
  • 1
    @Fallenreaper, right -- that's why I specified ISO 8601 **basic**, as opposed to ISO 8601 extended (which has the colons and dashes). – Charles Duffy May 10 '17 at 15:17
  • 1
    yeah, i just wanted to make a public note that isoString function for Python's Datetime would not return the ISO8601 basic format, but the extended, So its required to use some a formatter – Fallenreaper May 10 '17 at 18:51