简体   繁体   中英

OSError: [Errno 63] File name too long

I have a short script that needs to merge multiple MKV files into one every night.

import os, glob
import subprocess

concatenated_files = ""

os.chdir("/cams/")
for index, file in enumerate(glob.glob("*.mkv")):
    if index == 0:
        concatenated_files = file
    else:
        concatenated_files += " + " + file

# print(concatenated_files)

returncode = subprocess.call("mkvmerge -o out.mkv " + concatenated_files)

I am getting the following error with just a few files

OSError: [Errno 63] File name too long: 'mkvmerge -o out.mkv video21-06-28_09-12-08-51.mkv + video21-06-28_07-55-36-80.mkv + video21-06-28_09-52-05-79.mkv + video21-06-28_08-47-56-69.mkv + video21-06-28_09-15-04-34.mkv + video21-06-28_09-32-43-25.mkv

I am planning to merge hundreds of them, so not really sure how to sort this situation with Python. It works well from the shell.

subprocess.call() first argument "args" is a list .

So I think it should probably be like this:

  import os, glob
  import subprocess

  concatenated_files = ""

  os.chdir("/cams/")
  args = ["mkvmerge"]
  for index, file in enumerate(glob.glob("*.mkv")):
     args.append(file)


  args.append("-o")        
  args.append("out.mkv")        


  returncode = subprocess.call(args)

Maybe using a shell script with only three lines of code is more simple and more clear:

ls /cams/ | grep -E ".+\.mkv$" | while read filename; do
  filenames="${filenames} ${filename}"
done
mkvmerge -o out.mkv ${filenames}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM