I want to display a snippet of Python code as a colored framed box in a beamer presentation. Besides, I'd like to save the snippet in a file. The code below gets the job done:
\documentclass{beamer}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{tcolorbox}
\tcbuselibrary{minted}
\newtcblisting{pythoncode}[4][\normalsize]{%
listing engine=minted,minted style=colourful, minted language=python,
oversize, listing only,left=1mm,bottom=1mm,right=1mm,top=1mm,
minted options={fontsize=#1},
listing file={snippets/#2},
colframe=#3,
title={\color {#4} \texttt{#2}}}
\begin{document}
\begin{frame}[fragile]
\frametitle{Test}
\begin{pythoncode}[\small]{script.tex}{blue}{yellow}
def add(x, y):
'''Add two numbers'''
return x + y
result = add(x, y)
print(result)
\end{pythoncode}
\end{frame}
\end{document}
However, if I try to change the extension of the output file from .tex to .py:
\begin{pythoncode}[\small]{myscript.py}{blue}{yellow}
I get the following error:
! I can't write on file `snippets/myscript.py'.
\tcbverbatimwrite ...mediate \openout \tcb@out #1
\tcb@verbatim@begin@hook \...
l.2 ...honcode}[\small]{myscript.py}{blue}{yellow}
Please type another output file name
! Emergency stop.
\tcbverbatimwrite ...mediate \openout \tcb@out #1
\tcb@verbatim@begin@hook \...
l.2 ...honcode}[\small]{myscript.py}{blue}{yellow}
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on mwe.log.
How can I go about this?
EDIT
I tried other file extensions such as .c, .cpp, .m, and .java and the code works like a charm. It's only .py that produces the error.
I'm using MiKTeX 2.9 on a Windows 10 machine.

\listfiles? Also, make sure you have asnippetsfolder. – Phelype Oleinik Dec 08 '19 at 17:38snippetsfolder does exists andsnippets/myscript.texis saved without problem. You can download the full.logfile from here. – Tonechas Dec 08 '19 at 17:56snippets/myscript.pyis open in another program which might be locking the file (just guessing). – Phelype Oleinik Dec 08 '19 at 18:08snippets/myscript.pydoesn't even exist. This code worked for me a couple of months ago. I've been struggling desperately to fix it for two days but I can't get my head around this :( Any other pointers/suggestions would be greatly appreciated. – Tonechas Dec 08 '19 at 18:21! I can't write on filemeans that something outside of TeX's control is preventing it from writing to the file. For example, if I change the onwership of thesnippetsfolder to root (in Linux, I guess it's equivalent to Admin in Windows), then TeX can't write on it and that error shows up. To reduce variables, please try this code and tell me what happens:\newwrite\test\immediate\openout\test snippets/script.py\relax \csname stop\endcsname\bye– Phelype Oleinik Dec 08 '19 at 19:01