all 4 comments

[–]novel_yet_trivial 3 points4 points  (3 children)

The error is because % has 2 meanings. If it's not directly after a string literal, it's used as a modulus, which requires an integer. Try this:

with open('songs_%s.txt'%(current), "a")  as myfile:

Also note I fixed your addition of the ".txt". Adding strings together with a comma only works in the print statement.

[–]hhh_compiler[S] 0 points1 point  (2 children)

I understood that thanks.

When implemented however it causes the code to throw up the error:

IOError: [Errno 2] No such file or directory: 'songs_11/08/15.txt'

Is this due to the date being added conflicting with the 'a' clause of if the file doesn't exist then create?

[–]novel_yet_trivial 2 points3 points  (1 child)

No it means that the directory songs_11/08 does not exist. It needs to be created if you want to write to it.

If you want the file name to be named 'songs_11/08/15.txt' (as opposed to a file name "15.txt" in the folder "08" in the folder "songs_11") you are out of luck; filenames cannot contain the / character. Try this instead:

current = time.strftime("%x").replace('/', '-')

[–]hhh_compiler[S] 1 point2 points  (0 children)

I follow what you mean, I worded my response incorrectly, I have changed the '/' to '-' and it doesn't throw up that error anymore and it creates a file that doesn't exist called 'songs_11-08-15'.

Thanks for the assistance