文件未找到错误:[WinError 2]系统找不到指定的文件 - 在保存mp4图时,使用WinPython、Anaconda。

8
我有以下代码,当我运行它时,会出现错误 "FileNotFoundError: [WinError 2] 系统找不到指定的文件"。我已经添加了日志文件片段。奇怪的是,这个代码昨天在Anaconda上完美地工作。然而,在尝试在另一台电脑上运行它时(例如 WinPython 上的一台电脑),错误弹出了(当然,我已根据新机器调整了路径)。最糟糕的是,现在它甚至拒绝在“原始”开发代码和正常工作的机器上工作(没有更改任何内容,只重启了PC)。
也许有人遇到过同样的问题并找出了解决方案。感谢您的帮助!
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy import array
from matplotlib import animation
import os



jet = plt.get_cmap('jet')
fig = plt.figure()
ax = fig.gca(projection='3d')

X = np.linspace(70,40,4)
Y = np.linspace(5,2,4)
X,Y=  np.meshgrid(X, Y)

Z = array ([
    [1223.539555, 1428.075086,1714.479425, 2144.053223],
    [1567.26647,1829.056119,2990.416079,2745.320067],
    [2135.163957,2491.534201, 2990.416079,3738.761638],
    [3257.280827, 3800.655101, 4561.372117, 5702.458776],
    ])

surf = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap =jet,linewidth = 0,alpha= 1)
ax.set_zlim3d(0, Z.max())

fig.colorbar(surf, shrink=0.8, aspect=5)
ax.set_xlabel('Axial Length [mm]')
ax.set_ylabel('nbTurns')
ax.set_zlabel('RPM')

def rotate(angle):
    ax.view_init(azim=angle)

fig.set_size_inches(20, 20)
dpi = 150#asta
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
mywriter= animation.FFMpegWriter(fps=80)

folder = 'D:/IuliaVascan/Grafice/'
file = 'unt1.mp4'

path = os.path.join(folder, file)
rot_animation.save(path, writer=mywriter,dpi=dpi)

plt.show()

完整的回溯信息如下:
Traceback (most recent call last):

  File "<ipython-input-13-dfe90b3fa52e>", line 1, in <module>
    runfile('C:/Users/username/untitled6.py', wdir='C:/Users/username')

  File "C:\Users\username\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Users\username\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/username/untitled6.py", line 60, in <module>
    rot_animation.save(path, writer=mywriter,dpi=dpi)#asta

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

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

  File "C:\Users\username\Anaconda3\lib\site-packages\matplotlib\animation.py", line 200, in saving
    self.setup(*args)

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

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

  File "C:\Users\username\Anaconda3\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)

  File "C:\Users\username\Anaconda3\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified

请显示完整的错误信息--它应该给出找不到的文件名。你是如何运行这段代码的? - cdarke
嗨,我已经添加了日志文件。我想提一下,使用以下代码行也可以实现保存:rot_animation.save('D:/Pictures/animation.mp4',writer=mywriter,dpi=dpi) ,当然,Pictures文件夹存在,并且每次尝试创建新动画(例如使用DPI模式或不同的大小)时,animation.mp4都会被覆盖。它突然停止工作了,我无法解释。 - Iulia_Vascan
在我看来,matplotlib正在启动一个单独的进程来处理您的动画(请注意回溯中对subprocess.py的引用),而不是您要保存的文件名未被找到。 - jasonharper
1个回答

6
如果有人遇到同样的错误,我通过在我的环境中安装ffmpeg来解决了这个问题。我不知道为什么在安装matplotlib时它没有自动安装作为依赖项,但没关系。希望这可以帮助到大家。

2
我在使用"from matplotlib import animation"时遇到了相同的错误。安装ffmpeg(在我的情况下使用conda install ffmpeg)解决了这个问题。 - DivideByZero

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接