gpt4 book ai didi

python - 如何使用 ftplib 在远程服务器上创建 .txt 文件并使用变量在其中放置一些字符串?

转载 作者:行者123 更新时间:2023-12-01 01:05:40 24 4
gpt4 key购买 nike

我有一个在远程服务器上创建文件的功能,我想用一个条目(输入字段)填充该文件。我的问题是,当我使用这段代码时;

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

我收到一个错误消息,

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

我尝试过这样的事情;

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

但它不会将 str 与字节连接起来。有什么建议吗?我想从输入字段提供 textToWrite。当我从这样的代码中给出它时,它就起作用了;

io.BytesIO(b"Some text")

最佳答案

也许这可以解决

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)

关于python - 如何使用 ftplib 在远程服务器上创建 .txt 文件并使用变量在其中放置一些字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com