Search code examples
pythonioftpftplib

How to create .txt file on remote server using ftplib and place some string in it using a variable?


I have a function to create a file on remote server and I want to fill that file with an Entry (input field). My question is, when i use this code;

def createFileRemote():
    textToWrite = io.StringIO(create_file_remote_input_text.get())
    ftp.storbinary('STOR '+create_file_remote_filename.get(),textToWrite)

I got an error says,

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

I tried something like this;

textToWrite = io.BytesIO(b""+create_file_remote_input_text.get())

but it does not concatenate str with bytes. Any suggestion ? I want to give textToWrite from an input field. It's working when i give it from code like this;

io.BytesIO(b"Some text")

Solution

  • Maybe this solves it

    def createFileRemote():
        to_bytes = bytes(create_file_remote_input_text.get(), "UTF-8")
        textToWrite = io.BytesIO(to_bytes)
        ftp.storbinary('STOR '+create_file_remote_filename.get(),textToWrite)