Python &引用;系统找不到指定的文件";在matplotlib中使用ffmpeg设置动画时

Python &引用;系统找不到指定的文件";在matplotlib中使用ffmpeg设置动画时,python,windows,matplotlib,ffmpeg,Python,Windows,Matplotlib,Ffmpeg,我正在尝试使用我在家庭电脑(Windows 10)的另一台电脑(mac)上使用的函数,从一堆numpy阵列生成电影。以下是我正在使用的函数: def make_animation(frames,name): plt.rcParams['animation.ffmpeg_path'] = u'C:\ffmpeg-20190320-0739d5c-win64-static\bin\ffmpeg.exe' n_images=frames.shape[2] assert

我正在尝试使用我在家庭电脑(Windows 10)的另一台电脑(mac)上使用的函数,从一堆numpy阵列生成电影。以下是我正在使用的函数:

def make_animation(frames,name): 

    plt.rcParams['animation.ffmpeg_path'] = u'C:\ffmpeg-20190320-0739d5c-win64-static\bin\ffmpeg.exe' 
    n_images=frames.shape[2] 
    assert (n_images>1)   
    figsize=(10,10)
    fig, ax = plt.subplots(figsize=figsize)
    fig.tight_layout()
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
    #lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
    img = ax.imshow(frames[:,:,0], animated = True)   


    def updatefig(img_num): 

        #lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')

        img.set_data(frames[:,:,img_num])

        return [img]


    ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
    mywriter = animation.FFMpegWriter(fps = 20)
    #ani.save('mymovie.mp4',writer=mywriter)

    ani.save(f"D:\{name}.mp4",writer=mywriter)

    plt.close(fig)
以下是我得到的错误:

Traceback (most recent call last):

  File "<ipython-input-48-8861d4da3f36>", line 1, in <module>
    make_animation(stack,'full_test')

  File "<ipython-input-47-0e3683911f60>", line 27, in make_animation
    ani.save(f"D:\{name}.mp4",writer=mywriter)

  File "C:\Users\~snip~\Anaconda3\lib\site-packages\matplotlib\animation.py", line 1136, in save
    with writer.saving(self._fig, filename, dpi):

  File "C:\Users\~snip~\Anaconda3\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)

  File "C:\Users\~snip~\Anaconda3\lib\site-packages\matplotlib\animation.py", line 228, in saving
    self.setup(fig, outfile, dpi, *args, **kwargs)

  File "C:\Users\~snip~\Anaconda3\lib\site-packages\matplotlib\animation.py", line 352, in setup
    self._run()

  File "C:\Users\~snip~\Anaconda3\lib\site-packages\matplotlib\animation.py", line 363, in _run
    creationflags=subprocess_creation_flags)

  File "C:\Users\~snip~\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

  File "C:\Users\~snip~\Anaconda3\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)

  File "C:\Users\~snip~\Anaconda3\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
制作动画(堆栈,“完整测试”)
文件“”,第27行,在make_动画中
ani.save(f“D:\{name}.mp4”,writer=mywriter)
文件“C:\Users\~snip~\Anaconda3\lib\site packages\matplotlib\animation.py”,第1136行,保存
使用writer.saving(self.\u图、文件名、dpi):
文件“C:\Users\~snip~\Anaconda3\lib\contextlib.py”,第112行,输入__
返回下一个(self.gen)
文件“C:\Users\~snip~\Anaconda3\lib\site packages\matplotlib\animation.py”,第228行,保存
自我设置(图、输出文件、dpi、*args、**kwargs)
文件“C:\Users\~snip~\Anaconda3\lib\site packages\matplotlib\animation.py”,第352行,在安装程序中
self._run()
文件“C:\Users\~snip~\Anaconda3\lib\site packages\matplotlib\animation.py”,第363行,正在运行
creationflags=子流程\创建\标志)
文件“C:\Users\~snip~\Anaconda3\lib\site packages\spyder\u kernels\customize\spyderrcustomize.py”,第143行,在u init中__
super(子流程,self)。\uuuuu初始化(*args,**kwargs)
文件“C:\Users\~snip~\Anaconda3\lib\subprocess.py”,第775行,在\uu init中__
恢复信号,启动新会话)
文件“C:\Users\~snip~\Anaconda3\lib\subprocess.py”,第1178行,在执行子进程中
startupinfo)
FileNotFoundError:[WinError 2]系统找不到指定的文件

我知道这个代码基本上是有效的,因为我以前在另一台计算机上使用过它。我的猜测是ffmpeg的某些部分出错了,或者输出路径的某些部分出错了。我不确定ffmpeg会有什么问题,因为我肯定安装了它(通过conda),而且路径非常简单。另一方面,我不确定输出路径可能有什么问题。

我认为这种情况是因为文件路径错误

使用我在另一台计算机(mac)上使用过的功能

所以我相信前面的文件路径是用
/
表示的。现在您将其更改为
\
,因为Windows使用了不同的分隔符。看起来还好吧

没有

因为在python和大多数编程语言中,
\
是字符串的保留标志指示器。例如,
\n
表示新行。因此,您应该使用
\\
,例如
“D:\{name}.mp4”
而不是
“D:\{name}.mp4

您还可以使用
os.path.join
,示例可以在

https://www.geeksforgeeks.org/python-os-path-join-method/
但在这种情况下,您希望文件位于相同的路径,至少是相同的相对路径