python - How i can find DIR which is present in last in list -


import ftplib   import re import string ftp = ftplib.ftp('')    # access ftp link. ftp.login ("") ftpdir = ftp.cwd('') data=[] ftp.dir(data.append) line in data:     string.find("dir","data")     if "dir" in line:            print line 

with code got list of dir names present in ftp server, how can find dir present in last in list.

my output is:

12-09-11  10:04pm       <dir>          mcl_201147_hw79u_05     12-08-11  01:19am       <dir>          mcl_201148_hw79u_02     12-16-11  12:53pm       <dir>          mcl_201149_hw79u_07     12-25-11  12:11am       <dir>          mcl_201150_hw79u_05     01-07-12  12:00am       <dir>          mcl_201151_hw79u_04     01-07-12  11:37pm       <dir>          mcl_201152_hw79u_05     01-17-12  01:06am       <dir>          mcl_201201_hw79u_04     01-29-12  12:52am       <dir>          mcl_201202_hw79u_04     02-04-12  01:06am       <dir>          mcl_201203_hw79u_03     02-11-12  01:28am       <dir>          mcl_201204_hw79u_05     02-17-12  01:12am       <dir>          mcl_201205_hw79u_05     02-19-12  01:22am       <dir>          mcl_201206_hw79u_05     03-02-12  02:24am       <dir>          mcl_201207_hw79u_05     03-04-12  02:01am       <dir>          mcl_201208_hw79u_05     03-05-12  12:25pm       <dir>          mcl_201209_hw79u_06 

i want find last dir present in list (03-05-12 12:25pm mcl_201209_hw79u_06)

if list, this

try:   return data[-1] except indexerror e:   return e // in code can print "e" , can continue this...  // version 2 demonstrate why try, except useful  def getdir(data):    try:      data[-1]    except indexerror e:      return e  .... print getdir(data)     print 1+1 

suppose wrong indexing list, program not halt:

list index out of range 2 

is want?


to useful, should use logger keep track of e (error message), , instead, returns default value (like none) know there wrong / no directory exists.

and not sure what's use of string module here. use module if it's finding need in list.


please give me these descriptions , edit original post.

  1. give me results of print line
  2. give me result of print data
  3. what dir want?

while answering questions, can figure out how solve it.

thanks.


okay. assume each line record of form:

12-09-11 10:04pm <dir> mcl_201147_hw79u_05 assuming don't have way keep time, <dir>, , dir-name separate, this

mystring = "12-09-11  10:04pm       <dir>          mcl_201147_hw79u_05" dirname = mystring.split(" ")[-1] // ['12-09-11', '', '10:04pm', '', '', '', '', '', '', '<dir>', '', '', '', '', '', '', '', '', '', 'mcl_201147_hw79u_05'] // , [-1] indicates last element in list (in reverse order) 

you can try ' '.join(mystring.split()) first, apply split() (without argument), think doing split right away easier , more memory efficient first method create 1 list, , alternative method create 2 lists.

if lines not str type, try see if str(line) helps.

if ever need find dirname, please google list comprehension. it's considered more efficient loops. it's compact way of writing going achieve... with, of course, proper stripping per requirement.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -