gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:28:37 27 4
gpt4 key购买 nike

看看其他有类似问题的人,通常他们只有相对路径,但事实并非如此,我使用的是通过 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com