user2765654
user2765654

Reputation: 160

FileNotFoundError on python3, even though the File does exist

Looking at other people with a similar issue, it's usually that they only have a relative path, which is no the case, I'm using the full path which I'm getting with os.join().

This is my code:

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

The error happens here:

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

The full error message is this:

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'

As you can see, the path '/home/robert/Filme/sex school.mkv' is the full path, not just a relative one. When I try something like

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

on the command line, the file plays without issue, so the path is definitely correct. Why am I still getting this error though?

Upvotes: 1

Views: 1007

Answers (1)

wim
wim

Reputation: 362716

You don't need the shell extension quoting, because you are not renaming via the shell.

Ergo, remove this line:

filename2 = shlex.quote(filename)

And just use:

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

Now, enjoy your sex school video.

Upvotes: 2

Related Questions