I had the same problem, and I tried many different things (adding full paths to the bg.txt file, converting to UTF-8, ...) - which resulted in slightly different errors - but still to no avail. I follow this guide - and I prepare all the files on my Windows laptop, and then send them to the Ubuntu server.
, (CLRF LF), ( Ubuntu):
$ file bg.txt
bg.txt: UTF-8 Unicode (with BOM) text, with CRLF line terminators
dos2unix:
$ sudo apt install dos2unix
$ dos2unix bg.txt
dos2unix: converting file bg.txt to Unix format ...
$ file bg.txt
bg.txt: ASCII text
opencv_createsamples .