|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
searching FTP directory (getting foldernames as variable)
hi!
I'm trying to get a folder name in a ftp path into a variable. As I'm trying to get the newest folder, and I cant know what its called, I'm trying to access the info a: f.retrlines("LIST") returns... unfortunately only the server response gets stored... anybody know how to get the whole list into a string? or a tuple? or whatever? heres my test script: Code:
def latestFTP(country,city):
ftpHost="localhost"
ftpUser="chris"
ftpPasswd="chris"
mainPath = "etv/"
curDir = mainPath + country + "/" + city + "/"
f = ftplib.FTP(host=ftpHost,user=ftpUser,passwd=ftpPasswd)
f.cwd(curDir)
directorylist = f.retrlines("LIST")
print directorylist
f.quit()
even though the print returns the whole directory, only the server response is in the string. any suggestions gladly appreciated! Cheers Chris Last edited by goudaman : October 1st, 2003 at 09:27 AM. |
|
#2
|
||||
|
||||
|
I've looked at the docs on the ftplib module (http://www.python.org/doc/current/lib/ftp-objects.html). The problem is that retrlines() doesn't actually return it's results, instead it outputs them to sys.stdout.. when using the default callback function anyway.
On the other hand nthe lst() function seems to do what you want ![]() Quote:
Now i have NO idea what the NLST command is so i could be mistaken here , another aproch might be to use sendcmd()..Quote:
Mark. |
|
#3
|
|||
|
|||
|
thanks! I seem to have overread that!
now what do i do with the <PyShell.PseudoFile instance at 0x00A39C60> ? ![]() how can i access the "contents"? cheers a bunch! chris |
|
#4
|
||||
|
||||
|
Ah ok
, what returned that? What function did you go for.. post some more code dude ![]() Mark. |
|
#5
|
|||
|
|||
|
ok that one whas crap....
i put a Code:
print sys.stdout stupid enough... ok when i put: Code:
def latestFTP(country,city):
import sys
lastEntryTime = "031001"
ftpHost="localhost"
ftpUser="chris"
ftpPasswd="chris"
mainPath = "etv/"
curDir = mainPath + country + "/" + city + "/"
f = ftplib.FTP(host=ftpHost,user=ftpUser,passwd=ftpPasswd)
f.cwd(curDir)
directoryList = f.nlst(curDir)
print directoryList
f.quit()
lastEntryTime = "032023"
return lastEntryTime
returns: Code:
[] '032023' so wheres the contents for [] ... argh. i guess nlst lists files only and not the folders |
|
#6
|
|||
|
|||
|
got it... d'oh!
the f.cwd(curDir) and the f.nlst(curDir) didnt fit together... f.nlst() gives: ['031001', '031002', '031003'] (all the folder names) but cheers to you for the right anwser! chris |
|
#7
|
||||
|
||||
knew ya would, i was a lil shocked myself when you gave me a memory location i.e. <Py...>Python's great! but it does take a while to get to know it's stardard library, it's just so full.. and thats without adding some of the other nice modules, numarry, psyco, PIL, mysqldb, sqlite etc. Mark. |
|
#8
|
|||
|
|||
|
true true.... and I havent even scratched on those
![]() but one more thing: how about getting the "newest" file/folder ? even though the retrlines giver the date, i cant find a way to acces the date info... i guess i'm not looking properly.... any hints? cheers Chris |
|
#9
|
||||
|
||||
|
I can't see a function which will let you do this in ftplib, i think your best bet would be to use cwd() to set the current working directory and then use sendcmd() with 'LIST' or a similar ftp command to retrieve your data
, depending on how his data is returned it may need to be split apart etc.Have fun, Mark |
|
#10
|
|||
|
|||
|
hmm
the f.sendcmd("list") doesnt work gives me a 550 List failed. No port specified. From what i could find out, this is because of the passive mode. but even when i change that on the ftp server, its the same. dont i love it? i guess not |
|
#11
|
||||
|
||||
|
Ah ok, this makes total sence
, you could try passing port as an argument somewhere.. There's an optional acct argument with FTP but i dont know what thats for?I really have no idea is this a thing with your servers setup? Also you might look into an FTP command which returns the last modified if such a command exists.. in any case i think the answer lies in FTP commands, why retrlines() can't just return a value is beond me! ![]() If your feeling confident you could always crack open the ftplib module and make a few changes, i'm not exactly sure how it works since i havn't looked inside.. Anyway have fun Chris, Marky. |
![]() |
| Viewing: Dev Shed Forums > System Administration > FTP Help > searching FTP directory (getting foldernames as variable) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|