I am trying to make a FTP script in Python3.2 that connects to a server and downloads all the contents. While it handles files perfectly, directories seem to make things a bit troubling. I have made it so when it encounters the directory it will set the variable directory to the new directory and hopefully download it's contents. It will exclude an items already completed and if all the contents in directory are in the completed it will back of the directory until it finishes. While the code seems like it would work it doesn't. It seems I can't change directory mid session of a for loop. Here is the code: **Note some variables might be removed later, just want to get a rough draft going here
import os, time, math, ftplib, zipfile, socket ## Just the regular imports... I guess
session = ftplib.FTP("CENSORED.info")
session.login(user="CENSORED",passwd="CENSORED")
dbpath = "/home/CENSORED/Dropbox/CENSORED"
debuglevel=0
session.set_debuglevel(debuglevel)
if session.getwelcome() != "":
level = 1
completed = []
leveldir = {}
currentdir = session.pwd()
directory = session.nlst()
reset = False
print("CONNECTED")
print("")
while level != 0:
if reset == False:
level = 0
reset = True
try:
print(directory)
for file in directory:
if file not in completed:
try:
print("Getting",file)
savefile = open(os.path.join(dbpath,file), 'wb')
session.retrbinary("RETR %s" % file, savefile.write)
completed.append(file)
except ftplib.error_perm:
level += 1
leveldir[level] = currentdir
session.cwd(currentdir + file)
leveldir[level] = currentdir
directory = session.nlst()
print(leveldir,level)
print(file,"is a Directory! Attempting to enter...")
if directory in completed:
level -= 1
session.cwd(leveldir[level])
except IOError:
print("Could Not obtain directory...")
else:
print("")
print("DISCONNECTED")
session.close()
print("")
print("DISCONNECTED")
**Note I can't guarantee that the spacing is correct, it's very hard to tell in Stackoverflow's post editor. For a website about programming I think they should handle code snippets much better than they do.
Here is the error I get in console:
lib is a Directory! Attempting to enter...
Getting minecraft_server.jar
Traceback (most recent call last):
File "CENSOREDftpbackup.py", line 34, in <module>
session.retrbinary("RETR %s" % file, savefile.write)
File "/usr/local/lib/python3.2/ftplib.py", line 417, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File "/usr/local/lib/python3.2/ftplib.py", line 379, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "/usr/local/lib/python3.2/ftplib.py", line 342, in ntransfercmd
resp = self.sendcmd(cmd)
File "/usr/local/lib/python3.2/ftplib.py", line 255, in sendcmd
return self.getresp()
File "/usr/local/lib/python3.2/ftplib.py", line 229, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Can't open minecraft_server.jar: No such file or directory
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "CENSORED.py", line 40, in <module>
session.cwd(currentdir + file)
File "/usr/local/lib/python3.2/ftplib.py", line 556, in cwd
return self.voidcmd(cmd)
File "/usr/local/lib/python3.2/ftplib.py", line 260, in voidcmd
return self.voidresp()
File "/usr/local/lib/python3.2/ftplib.py", line 234, in voidresp
resp = self.getresp()
File "/usr/local/lib/python3.2/ftplib.py", line 229, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Can't change directory to /minecraft_server.jar: Not a directory
Thanks!