Skip to content
Advertisement

Python inserting variable string as file name

I’m trying to create a file with a unique file name for every time my script runs. I am only intending to do this to every week or month. so I chose to use the date for the file name.

JavaScript

is where I’m getting this error.

JavaScript

it works if I use a static filename, is there an issue with the open function, that means you can’t pass a string like this?

name is a string and has values such as :

JavaScript

Many thanks for any help.

Advertisement

Answer

You need to put % name straight after the string:

JavaScript

The reason your code doesn’t work is because you are trying to % a file, which isn’t string formatting, and is also invalid.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement