python - python3 上的 FileNotFoundError,即使文件确实存在

标签 python python-3.x

看看其他有类似问题的人,通常他们只有相对路径,但事实并非如此,我使用的是通过 os.join() 获得的完整路径。

这是我的代码:

def reencode(file):
global DEFAULT_GFORMAT
global DEFAULT_VCODEC
global DEFAULT_ACODEC
global containerformat
global input
filename = os.path.join(input, file)

Container = 0
Video = 0
Audio = 0

if changeContainer:  # The container needs to be changed
    Container = DEFAULT_GFORMAT
else:  # Container is already fine
    Container = "copy"
if reencodeVideo:  # Video codec needs to be changed
    Video = DEFAULT_VCODEC
else:  # Video codec is already fine
    Video = "copy"
if reencodeAudio:  # Audio codec needs to be changed
    Audio = DEFAULT_ACODEC
else:  # Audio codec is already fine
    Audio = "copy"
if(Container == "copy" and Video == "copy" and Audio == "copy"):
    print("{}: File is playable by the fire tv stick".format(file))
    print()
else:  # File needs to be reencoded
    name = os.path.splitext(file)[0]  # This removes the file extension from the name
    if(containerformat == "MPEG-4"):  # We only need to check for mp4 since if it's not mp4 it's either mkv or it's gonna be reencoded to mkv
        newName = name + ".mp4"  # This is the new name for the output file
    else:
        newName = name + ".mkv"  # This is the new name for the output file
    print("Reencoding file...")
    filename2 = shlex.quote(filename)
    print(filename2)
    os.rename(filename2, (filename + ".bak"))  # File needs to be renamed so it's not overwritten
    x = (input) + ".bak"
    y = shlex.quote(x)
    z = shlex.quote(newName)
    command = "ffmpeg -loglevel error -stats -i " + y + " -map 0 -scodec copy -vcodec " + Video + " -acodec " + Audio + " " + z
    try:
        subprocess.call(command, shell=True)  # Run the command
    except OSError as e:  # Some error happened
        if e.errno == os.errno.ENOENT:  # ffmpeg is not installed
            print("ffmpeg does not seem to be installed. Please install it if you want to run this script")
        else:  # Something else went wrong
            raise
    print()  # Just to make it look a little nicer

错误发生在这里:

os.rename(filename2, (filename + ".bak"))

完整的错误消息是这样的:

line 153, in reencode
os.rename(filename2, (filename + ".bak"))  # File needs to be renamed so it's not overwritten
FileNotFoundError: [Errno 2] No such file or directory: "'/home/robert/Filme/sex school.mkv'" -> '/home/robert/Filme/sex school.mkv.bak'

如您所见,路径“/home/robert/Filme/sex school.mkv”是完整路径,而不仅仅是相对路径。当我尝试类似的事情时

mpv '/home/robert/Filme/sex school.mkv'

在命令行上,文件播放没有问题,所以路径肯定是正确的。为什么我仍然收到此错误?

最佳答案

您不需要引用 shell 扩展名,因为您不是通过 shell 进行重命名。

因此,删除此行:

filename2 = shlex.quote(filename)

只需使用:

os.rename(filename, filename + ".bak")

现在,欣赏您的性学校视频。

关于python - python3 上的 FileNotFoundError,即使文件确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32738681/

相关文章:

python - 通过 dict of dict of dict 计算值

python - Django View 只渲染一页,不会渲染其余页面

python - 在多级数据框中创建条件列

Python 3 枚举 : Enum inheriting another Enum doesn't work?

python - gevent-websocket python WAMP客户端?

python - 在python中将图像转换为csv文件

python - 为什么 easygui 在传递包含带嵌入空格的字符串的元组时插入大括号?

python - 如何在不创建任何新变量的情况下在字典理解中解压缩元组的值?

python - matplotlib 中的 set_xlim() 和 set_ylim() 是什么?

python-3.x - 如何使用 google colab 在文本单元格中插入变量