R1CH101 R1CH101 - 3 months ago 9
Python Question

ftp videos using python3 with ftplib

Hi I am trying to upload a video to my web server using ftplib with python but it is not working it comes up with encoding error

here is my code

from ftplib import FTP

ftp = FTP('ftp.example.com')
ftp.login(user='ftp@example.com', passwd='password')
ftp.storbinary("STOR video.mp4", open("video.mp4", 'r'))


and here is the outcome:

Traceback (most recent call last):
File "/home/pi/Desktop/filerepeater.py", line 50, in <module>
ftp.storbinary("STOR video.mp4", open("video.mp4", 'r'))
File "/usr/lib/python3.2/ftplib.py", line 471, in storbinary
buf = fp.read(blocksize)
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9b in position 30: invalid start byte


used code
ftp.storbinary("STOR video.mp4", open("video.mp4", 'rb'))
to fix

Answer

My guess is you need to open the video file with mode 'rb':

ftp.storbinary("STOR video.mp4", open("video.mp4", 'rb'))
Comments