Verfasst: Dienstag 5. Mai 2009, 15:46
Ich hatte es mir über help(ftplib.FTP) ausgeben lassen und mich an der Ausgabe nicht weiter gestört.
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
import os
import FTPFileSorter
from operator import itemgetter
from ftplib import FTP
ftp = FTP("dadada") # connect to host, default port
ftp.login("pipipi","dididi") # user anonymous, passwd anonymous@
ftp.cwd("/dat3")
#ftp.retrlines('LIST') # list directory contents
# Define the local directory name to put data in
ddir="C:\\datadir"
# If directory doesn't exist make it
if not os.path.isdir(ddir):
os.mkdir(ddir)
# Change the local directory to where you want to put the data
os.chdir(ddir)
Code: Alles auswählen
from snafu_ftp import FTPFileSorter
Code: Alles auswählen
ftp = FTPFileSorter("dadada")
Code: Alles auswählen
>>> import os
>>> os.path.exists("test.py")
True
>>> os.path.isfile("test.py")
True
>>> os.path.isdir("test.py")
False
Code: Alles auswählen
from ftpsorter import FTPFileSorter as Sorter
sorter = Sorter(adresse_von_ftpserver, dein_username, dein_pw) # natürlich nur wenn du nen username + passwort da hast, ansonsten nur die adresse
sorter.cwd('/dat3')
# das listet dir die dat-files geordnet nach datum auf:
sorter.datesort_files(pattern='*.dat', prettylines=True)
# wenn du mit dem dateinamen weiterarbeiten willst:
latest_dat, date = sorter.datesort_files(pattern='*.dat')[0]
Code: Alles auswählen
import os
from ftpsorter import FTPFileSorter as Sorter
sorter = Sorter("bububu", "dadada", "trtrtr")
sorter.cwd('/dat3')
# das listet dir die dat-files geordnet nach datum auf:
sorter.datesort_files(pattern='*.dat', prettylines=True)
# wenn du mit dem dateinamen weiterarbeiten willst:
latest_dat, date = sorter.datesort_files(pattern='*.dat')[0]
# Define the local directory name to put data in
ddir="C:\\datadir"
# If directory doesn't exist make it
if not os.path.isdir(ddir):
os.mkdir(ddir)
# get the remote file to the local directory
sorter.retrbinary("RETR %s" % latest_dat, open(latest_dat,"wb").write)
Code: Alles auswählen
# Change the local directory to where you want to put the data
os.chdir(ddir)
Code: Alles auswählen
import ftplib
from operator import itemgetter
class Sorter(ftplib.FTP):
def __init__(self, host='', user='', passwd='', acct=''):
"""
Behaves like ftplib.FTP()
"""
ftplib.FTP.__init__(self, host, user, passwd, acct)
def datesort_files(self):
"""
Return a date-sorted file list from the current
ftp directory with the latest file on top
"""
files = []
for filename in self.nlst():
try:
mdtm = self.sendcmd('MDTM %s' % filename)
date = mdtm.split()[1]
files.append((filename, date))
except ftplib.error_perm:
pass
return sorted(files, key=itemgetter(1), reverse=True)
Code: Alles auswählen
import os
from string import join as join
from shutil import rmtree as rmtree
from ftpsorter import *
from easygui import integerbox as integerbox
#Read data from config File
configfile = "config.csv"
exportpath = []
fpath = []
pdevice = []
plogin = []
ppassw = []
for line in open(configfile ,'rU'):
line = line.rstrip()
if line:
data = line.split(";")
exportpath.append(data)
fspath.append(data)
pdevice.append(data)
plogin.append(data)
ppassw.append(data)
exportpath = exportpath[0][0]
fpath = fpath[1][0]
pdevice = pdevice[2][0]
plogin = plogin[2][1]
ppassw = ppassw[2][2]
x=integerbox(msg=boxtext, title='fFTP Imp V0.1', default=1, argLowerBound=0, argUpperBound=99, image=None, root=None)
sorter = Sorter(pdevice ,plogin , ppassw)
sorter.cwd('/dat3')
#Delete path
rmtree(exportpath,1)
# das listet dir die dat-files geordnet nach datum auf:
sorter.datesort_files(pattern='*.dat', prettylines=True)
for x in range (0,x):
# wenn du mit dem dateinamen weiterarbeiten willst:
latest_dat, date = sorter.datesort_files(pattern='*.dat')[x]
latest_bin, date = sorter.datesort_files(pattern='*.bin')[x]
# If directory doesn't exist make it
if not os.path.isdir(exportpath):
os.mkdir(exportpath)
# Change the local directory to where you want to put the data
os.chdir(exportpath)
#write file to selected directory
sorter.retrbinary("RETR %s" % latest_dat, open(latest_dat,"wb").write)
sorter.retrbinary("RETR %s" % latest_bin, open(latest_bin,"wb").write)
file = open("datei" + str(x)+".txt",( "w") )
file.write(latest_dat)
x=x+1
file.close()
#cv.wait(10)
os.startfile(fpath)
cofi hat geschrieben:Ich glaube du solltest mal das Tutorial lesen.
Code: Alles auswählen
import ftplib
import time
class TimeFTP(ftplib.FTP):
def last_modified(self, *args):
for filename in self.nlst(*args):
retcode, timestamp = self.sendcmd('mdtm ' + filename).split(' ', 1)
timestruct = time.strptime(timestamp, '%Y%m%d%H%M%S')
yield filename, timestruct
Code: Alles auswählen
In [1]: from timeftp import TimeFTP
In [2]: ftp = TimeFTP('ftp.gimp.org', 'anonymous')
In [3]: ftp.cwd('pub/pango/1.21')
Out[3]: '250 Directory successfully changed.'
In [4]: max(ftp.last_modified('*.md5'))
Out[4]:
('pango-1.21.5.tar.gz.md5',
time.struct_time(tm_year=2008, tm_mon=8, tm_mday=26, tm_hour=21, tm_min=59, tm_sec=10, tm_wday=1, tm_yday=239, tm_isdst=-1))
Code: Alles auswählen
for x in range (0,x):
# wenn du mit dem dateinamen weiterarbeiten willst:
latest_dat, date = sorter.datesort_files(pattern='*.dat')[x]
latest_bin, date = sorter.datesort_files(pattern='*.bin')[x]
Code: Alles auswählen
letztes=max(ftp.last_modified('*.dat'))
ftp.retrbinary("RETR %s" "test %s", open(letztes, 'wb').write)
Code: Alles auswählen
In [10]: ftp = TimeFTP('ftp.gimp.org', 'anonymous')
In [11]: ftp.cwd('pub/pango/1.21')
Out[11]: '250 Directory successfully changed.'
In [12]: latest3 = sorted(ftp.last_modified('*.md5'))[-3:]
In [13]: latest3
Out[13]:
[('pango-1.21.4.tar.gz.md5',
time.struct_time(tm_year=2008, tm_mon=8, tm_mday=11, tm_hour=23, tm_min=58, tm_sec=36, tm_wday=0, tm_yday=224, tm_isdst=-1)),
('pango-1.21.5.tar.bz2.md5',
time.struct_time(tm_year=2008, tm_mon=8, tm_mday=26, tm_hour=21, tm_min=59, tm_sec=10, tm_wday=1, tm_yday=239, tm_isdst=-1)),
('pango-1.21.5.tar.gz.md5',
time.struct_time(tm_year=2008, tm_mon=8, tm_mday=26, tm_hour=21, tm_min=59, tm_sec=10, tm_wday=1, tm_yday=239, tm_isdst=-1))]
In [14]: for name, time in latest3: print name
....:
pango-1.21.4.tar.gz.md5
pango-1.21.5.tar.bz2.md5
pango-1.21.5.tar.gz.md5
Code: Alles auswählen
ftp.retrbinary("RETR %s" % latestx_dat, open(latestx_dat,"wb").write)
Code: Alles auswählen
#Import Bibliotheken
from timeftp import TimeFTP
ftp = TimeFTP('kkkk, 'pppp,'tttt') # Aufbau FTP Verbindung
ftp.cwd('/dat3') #In Quellverzeichnis wechseln
x=-2
latest_dat, last1 = max(ftp.last_modified('*.dat'))
latestx = sorted(ftp.last_modified('*.dat'))[x:]
for latestx_dat, time in latestx: latestx_dat
for name, time in latest3: print name
ftp.retrbinary("RETR %s" % latestx_dat, open(latestx_dat,"wb").write)
Code: Alles auswählen
In [1]: import timeftp
In [2]: ftp_addr = "ftp://ftp.gimp.org/pub/pango/1.21/"
In [3]: timeftp.get_last_modified(ftp_addr, '*.md5', 3, 'testdir', 'anonymous')