Python 具有字符编码和行尾选项的SFTP

Python 具有字符编码和行尾选项的SFTP,python,Python,我正在使用python将一个文件从一个Linux虚拟机发送到另一个Linux虚拟机,这非常好。文件已成功发送,但在发送文件之前,我想将文件编码更改为“UTF-8”,并将行尾更改为“Unix/Linux”。怎么做 下面是通过sftp发送文件的代码段: with pysftp.Connection(host=host, username=userName, password=passWord) as sftpVal: #print(sftpVal.listdir()) #list direc

我正在使用python将一个文件从一个Linux虚拟机发送到另一个Linux虚拟机,这非常好。文件已成功发送,但在发送文件之前,我想将文件编码更改为“UTF-8”,并将行尾更改为“Unix/Linux”。怎么做

下面是通过sftp发送文件的代码段:

with pysftp.Connection(host=host, username=userName, password=passWord) as sftpVal:
    #print(sftpVal.listdir()) #list directories in sftp home
    sftpVal.put(source_file_path,'incoming/'+fileName) #(localPath, destinationPath)

一种选择是读取文件内容,根据需要进行更改,将其保存到临时文件,然后发送临时文件。例如:

with open('file_name.txt') as original_file, open('file_to_send.txt', 'w') as send_file:  # Open the original and prepared files
    content = original_file.read()
    content = content.replace('\r\n', '\n')  # Change the line endings
    content = content.encode('utf-8')  # Encode in UTF-8
    send_file.write(content)

# Do your file send here but with the prepared file instead.

os.remove('file_to_send.txt')  # Optional removal of temp file.