Search code examples
pythontemporary-files

Getting a ValueError when making a NamedTemporaryFile in python


haven't found a solution to this through searching existing questions, here goes:

New-ish to python. Trying to make a temporary file using the tempfile package. Here is the line of code that is failing with a ValueError:

(temp_file, self.bucket) = tempfile.NamedTemporaryFile(suffix='.py', prefix='Custom_', dir=[mydir], delete=False)

I'm getting this ValueError when I run my script:

ValueError: need more than 0 values to unpack

Why?


Solution

  • tempfile.NamedTemporaryFile() doesn't return the filename, only the handle. You need to access the name attribute in order to get the filename.