Problems using ftplib

here is my code:

import csv
import urllib.request
from ftplib import FTP
import os
import sys

url = 'https://dd.weather.gc.ca/hydrometric/csv/SK/hourly/SK_hourly_hydrometric.csv'
response = urllib.request.urlopen(url)
lines = [l.decode('utf-8') for l in response.readlines()]
sk = csv.reader(lines)

print("Python version")
print(sys.version)


import ftplib
session = ftplib.FTP('www.test.org','user','password')
file = open('D:/code/WaterAutomate/venv/current.csv','rt')                  # file to send
session.cwd('www')
session.storlines("STOR sk.csv",file)     # send the file
file.close()                                    # close file and FTP
session.quit()

here is the result

PS D:\code\WaterAutomate\venv> d:; cd ‘d:\code\WaterAutomate\venv’; & ‘c:\Python312\python.exe’ ‘c:\Users\RobMe.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/…/…\debugpy\launcher’ ‘39474’ ‘–’ ‘d:\code\WaterAutomate\venv\test.py’
Python version
3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
Traceback (most recent call last):
File “d:\code\WaterAutomate\venv\test.py”, line 20, in
session.storlines(“STOR sk.csv”,file) # send the file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “c:\Python312\Lib\ftplib.py”, line 526, in storlines
if buf[-1] in B_CRLF: buf = buf[:-1]
^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not ‘str’
PS D:\code\WaterAutomate\venv>

the file I am trying to upload is test so it should be str? What am I doing wrong?

For the following line:

file = open('D:/code/WaterAutomate/venv/current.csv','rt') 

the t, implies text mode. Try rb for bynary-mode.

file = open('D:/code/WaterAutomate/venv/current.csv','rb') 

or try just r. Since from the error:

TypeError: a bytes-like object is required, not ‘str’

Also, curious if you have to use the r' for the backward slash:

file = open(r'D:\code\WaterAutomate\venv\current.csv','r') 

that seems to have done it but this is a text file …can you force text/ascii?

Can you try appending the following:

file = open('D:/code/WaterAutomate/venv/current.csv','r', encoding = 'UTF-8') 

As this says it wants a bytes-like object, just change:

file = open('D:/code/WaterAutomate/venv/current.csv','rt') 

to

file = open('D:/code/WaterAutomate/venv/current.csv','rb') 

rt - read text mode, which is the default for r, I don’t really know why it still exists
rb - read binary, will return a byte-like object when read