The company arranged for me to use RPA to download the exposure, sales and other data related to the live broadcast and video of each e-commerce platform yesterday. I basically completed it in one week with RPA. Finally, I used the ftp file upload tool that comes with Shadow RPA. All of them were specified on the ftp server. After uploading with RPA, the file names were all garbled. The default encoding of ftplib embedded in RPA is Latin encoding, and uploading Chinese is garbled. There are no other settings for the default component (for example, encoding). So I was forced to give up the on-site components and write the code by myself in order to solve the problem. Baidu said:
1. Before trying to upload, follow the prompts to convert the file name to utf-8.
def upload(f, remote_path, local_path): fp = open(local_path, "rb") #,encoding='utf-8' buf_size = 1024 # ("STOR {}".format(remote_path), fp, buf_size) # ("STOR {}"+remote_path, fp, buf_size) # ("STOR {}".format(remote_path)+r"/20250413_Tik Tok_XXX Clothing Flagship Store_Video Cooperation Video Data_1744606680.xlsx", fp, buf_size) # ("STOR "+remote_path+r"/", fp, buf_size) #Single file ok filenam2=local_path[local_path.rfind('\\')+1:].strip() print(filenam2+"#"+remote_path+"#"+local_path) filenam3=('utf-8').decode('iso-8859-1') ("STOR "+remote_path+filenam3, fp, buf_size) ()
The garbled code is still there and does not work.
2. There are people who say that they are changing the source code on the Internet. I am using Shadow Knife RPA here. I will find its source code and search for it on the computer D drive. Finally, before I changed the source code, I found a better solution, so I didn't try this method again.
3. Setting the encoding to general utf-8 is still not OK
4. Setting the encoding into general Latin is still not possible:
="iso-8859-1"
5. Read the document and say that you can use paramiko. I have finished installing it. Test:
def upload_file_via_ftp(hostname, port, username, password, local_file_path, remote_file_path): # Create an SSH client instance client = () # Automatically add policies to save server hostname and key information # client.set_missing_host_key_policy(()) try: # Connect to the server (hostname, port, username, password) # Use SFTPClient for file transfer sftp = client.open_sftp() (local_file_path, remote_file_path) print(f"document {local_file_path} Uploaded successfully {remote_file_path}") # Close SFTP connection () except Exception as e: print(f"上传document时发生错误: {e}") finally: # Close SSH connection ()
Report:
Error reading SSH protocol banner
I don’t know which software to set the ftp function on the ftp server, I don’t know whether I support ssh. If it doesn’t support it, I’ll waste my time.
6. I read the document and said that pysftp supports Chinese. I installed it and tested:
def main(args): with (glv['gvarFtpSerIP'], username=glv['gvarFtpUid'], password=glv['gvarFtpPs'],port=30000) as sftp: # ('Remote Chinese file.txt', 'local file.txt') # Download (r"C:\20250413_Tik Tok_XXX Clothing Flagship Store_Video Cooperation Video Data_1744606680.xlsx", '20250413_Tik Tok_XXXX Clothing Flagship Store_Video Cooperation Video Data_1744606680.xlsx') # Upload ()
Report: 'Connection' object has no attribute '_sftp_live' , I checked it around, but I couldn't find a method similar to login and openftp. There is no good solution for the time being
7. Let’s look at the embedded ftplib. I used to set utf-8. I had a sudden idea, why didn’t I set gbk? So test it out and it works.
The final code is as follows:
from ftplib import FTP def upload(f, remote_path, local_path): fp = open(local_path, "rb") #,encoding='utf-8' buf_size = 1024 # ("STOR "+remote_path+r"/", fp, buf_size) #Pure English file name ok filenam2=local_path[local_path.rfind('\\')+1:].strip() print(filenam2+"#"+remote_path+"#"+local_path) ("STOR "+remote_path+filenam2, fp, buf_size) () def main(args): ftp = FTP() ='gbk'# ok The most important here (glv['gvarFtpSerIP'], 30000) # The first parameter can be the IP or domain name of the ftp server, and the second parameter is the connection port of the ftp server, which defaults to 21 ftp.set_debuglevel(2) (glv['gvarFtpUid'], glv['gvarFtpPs']) # Use anonymous login directly () (glv['gvarRemotPath']) # Switch to the tmp directory upload(ftp,"/sjzt/pp/ds/douyin/",r"C:\[20250411]_[Tik Tok]_[XXXX Clothing Flagship Store]_[Video Cooperation Video Data]_[1744461854].xlsx") # Upload the files in the current directory to the tmp directory of the ftp server and name them ftp_a.txt # The above is also Ok # download(ftp, "ftp_a.txt", "") # Download the ftp_a.txt file in the ftp server tmp directory to the current directory and name it ()
This is the end of this article about the solution to uploading garbled file names in python ftplib. For more related contents in python ftplib uploading garbled file names, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!