| | |
ftplib and the retrbinary() command
![]() |
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Solved Threads: 0
I'm trying to finish up my first Python program. The program was built to help me maintain and add content to my webpage (it's nothing fancy...just pure html). Everything was smooth sailing (hey, python kicks some serious butt), until I ran into problems using retrbinary() to download files. The problem lies in the FTPDown() function of my program. When I try to execute the funcition i get a fun error message:
I've been hunting around, and everything seems to indicate that there should be nothing wrong with my code. Obviously, though, something is.
I'm running Python 2.4.1 on Win2k.
Could someone explain what I'm doing wrong here? Oh, and also, i'd like to know the os command for clear screen in windows. Seems like 'cls' should work somewhere, but....
code follows:
---
so long and thanks for all the fish
Python Syntax (Toggle Plain Text)
Username and Password successfully verified 220 FTP Server ready Downloading last.htm Traceback (most recent call last): File "HTMLFixer.py", line 303, in ? MainMenu() File "HTMLFixer.py", line 212, in MainMenu FTPDown() File "HTMLFixer.py", line 46, in FTPDown myftp.storlines('RETR '+filename,fileOut.write) File "C:\Documents and Settings\tbeck\My Documents\tools\pt\lib\ftplib.py", line 428, in storlines buf = fp.readline() AttributeError: 'builtin_function_or_method' object has no attribute 'readline'
I've been hunting around, and everything seems to indicate that there should be nothing wrong with my code. Obviously, though, something is.
I'm running Python 2.4.1 on Win2k.
Could someone explain what I'm doing wrong here? Oh, and also, i'd like to know the os command for clear screen in windows. Seems like 'cls' should work somewhere, but....
code follows:
Python Syntax (Toggle Plain Text)
from ftplib import FTP import os import re import sys import fnmatch def FTPDown(): ftplocation=raw_input("Please Enter the FTP location: ") print "Connecting to "+ftplocation try: myftp=FTP(ftplocation) except: print "Could not connect to "+ftplocation raw_input("--Press any key to return to main menu--") ClearScreen() return print "Connected to "+ftplocation myftp.set_debuglevel(0) username=raw_input("Please Enter Username: ") password=raw_input("Please Enter Password: ") try: myftp.login(username,password) except: print "Login Failed: Username or Password is incorrect" myftp.close() raw_input("--Press any key to return to main menu--") ClearScreen() return ClearScreen() print "Username and Password successfully verified" print myftp.getwelcome() fileList = myftp.nlst() targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for filename in targetList: print "Downloading "+filename try: fileOut=open(filename,'wb') except: print "couldn't open the output file" myftp.storlines('RETR '+filename,fileOut.write) fileOut.close() myftp.close() def FTPUp(): ftplocation=raw_input("Please Enter the FTP location: ") print "Connecting to "+ftplocation try: myftp=FTP(ftplocation) except: print "Could not connect to "+ftplocation raw_input("--Press any key to return to main menu--") ClearScreen() return print "Connected to "+ftplocation myftp.set_debuglevel(0) username=raw_input("Please Enter Username: ") password=raw_input("Please Enter Password: ") try: myftp.login(username,password) except: print "Login Failed: Username or Password is incorrect" myftp.close() raw_input("--Press any key to return to main menu--") ClearScreen() return ClearScreen() print "Username and Password successfully verified" print myftp.getwelcome() filePath = os.getcwd() fileList = os.listdir(filePath) targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for filename in targetList: fileUp=open(filename,'rb') print "Uploading "+filename myftp.storbinary('STOR '+filename, fileUp) fileUp.close() myftp.close() def StripServerComments(wholeFile): partFile = "" curLine="1" inBlock=0 while curLine: while inBlock: curLine=wholeFile.readline() commentPattern = re.compile(r'<!.*preceding.*>') if commentPattern.search(curLine): inBlock=0 curLine=wholeFile.readline() commentPattern = re.compile(r'<!.*following.*>') if commentPattern.search(curLine): inBlock=1 else: commentPattern = re.compile(r'<!.*below.*>') if commentPattern.search(curLine): curLine="" else: partFile=partFile+curLine commentPattern = re.compile(r'<!.*above.*>') if commentPattern.search(curLine): partFile="" partFile=partFile+"</HTML>" return partFile def InsertLink(wholeFile,target,newURL,linkName): if target=="visitors": print "Links may not be inserted after "+target return wholeFile.read() partFile = "" curLine="1" notFound=1 while curLine: curLine=wholeFile.readline() commentPattern = re.compile(r'^<p.*>-?'+target+'.*') if commentPattern.search(curLine): notFound=0 partFile=partFile+curLine curLine=wholeFile.readline() partFile=partFile+curLine partFile=partFile+'<p align=center><FONT size=4>~<a href="'+newURL+'">'+linkName+'</a>~</p>\n<BR>\n' else: partFile=partFile+curLine if notFound: print "The insertion point, "+target+", could not be found. "+wholeFile.name+" was not altered" else: print linkName+" successfully inserted into "+wholeFile.name return partFile def DeleteLink(wholeFile,target): if target=="home" or target=="visitors": print "Target "+target+" may not be deleted. This is a permanent item." return wholeFile.read() partFile = "" curLine="1" notFound=1 while curLine: curLine=wholeFile.readline() commentPattern = re.compile(r'^<p.*>-?'+target+'.*') if commentPattern.search(curLine): notFound=0 curLine=wholeFile.readline() else: partFile=partFile+curLine if notFound: print "The target for deletion, "+target+", was not found. "+wholeFile.name+" unaltered" else: print target+" successfully deleted from "+wholeFile.name return partFile def ChangeItem(wholeFile,targetItem,newItem): partFile = "" curLine="1" while curLine: curLine=wholeFile.readline() commentPattern = re.compile(targetItem) newLine=commentPattern.sub(newItem,curLine) partFile=partFile+newLine return partFile def MainMenu(): print "HtmlFixer 1.1" print " Scripted by Tucker A. Beck" print " For questions/comments email to nax13@yahoo.com" menuInput="a" while menuInput!="9": print "--------------------------------------" print "1: Insert a new side-link" print "2: Delete a side-link" print "3: Clean up server generated comments" print "4: Replace text block" print "7: Download files from FTP" print "8: Upload files to FTP" print "9: Quit" print "--------------------------------------" menuInput=raw_input(" : ") if menuInput=="1": InsertMenu() if menuInput=="2": DeleteMenu() if menuInput=="3": StripFiles() if menuInput=="4": ChangeMenu() if menuInput=="7": FTPDown() if menuInput=="8": FTPUp() def InsertMenu(): target=raw_input("Insert after which item? ") newURL=raw_input("File name for new item? ") linkName=raw_input("Link title for new item? ") filePath=os.getcwd() fileList = os.listdir(filePath) targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for fileName in targetList: oldFile=open(fileName) newFile=InsertLink(oldFile,target,newURL,linkName) oldFile.close() saveout=sys.stdout fileOut=open(fileName,'w') sys.stdout=fileOut print newFile fileOut.close() sys.stdout=saveout def DeleteMenu(): target=raw_input("Delete which item? ") filePath=os.getcwd() fileList = os.listdir(filePath) targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for fileName in targetList: oldFile=open(fileName) newFile=DeleteLink(oldFile,target) oldFile.close() saveout=sys.stdout fileOut=open(fileName,'w') sys.stdout=fileOut print newFile fileOut.close() sys.stdout=saveout def StripFiles(): ClearScreen() filePath=os.getcwd() fileList = os.listdir(filePath) targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for fileName in targetList: oldFile=open(fileName) newFile=StripServerComments(oldFile) oldFile.close() saveout=sys.stdout fileOut=open(fileName,'w') sys.stdout=fileOut print newFile fileOut.close() sys.stdout=saveout print "Stripped server generated comments from "+fileName def ChangeMenu(): targetItem=raw_input("Change what text? ") newItem=raw_input("Change to what? ") filePath=os.getcwd() fileList = os.listdir(filePath) targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')] for fileName in targetList: oldFile=open(fileName) newFile=ChangeItem(oldFile,targetItem,newItem) oldFile.close() saveout=sys.stdout fileOut=open(fileName,'w') sys.stdout=fileOut print newFile fileOut.close() sys.stdout=saveout print "Updated '"+targetItem+"' to '"+newItem+"' in "+fileName def ClearScreen(): for i in range(30): print "" ClearScreen() MainMenu() ClearScreen() print "Thanks for using HTMLFixer by Tucker Beck" print "For questions or comments, please contact nax13@yahoo.com" ext=raw_input("--press enter to exit--")
---
so long and thanks for all the fish
![]() |
Similar Threads
- C command-line I/O question (C++)
- DOS "ROUTE" command (Visual Basic 4 / 5 / 6)
- command line (OS X)
- Graphics Card issues Stop Command??? (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: my pride and joy
- Next Thread: Regular Exp
| Thread Tools | Search this Thread |
alarm anydbm app assignment beginner bluetooth character cipher cmd conversion coordinates corners curves customdialog cx-freeze data decimals definedlines development directory events excel exe feet file float format function generator getvalue gnu halp handling homework http ideas input ip itunes keycontrol leftmouse line linux list lists loan loop maintain maze millimeter module mouse number numbers output parsing path prime programming push py2exe pygame pymailer python queue random rational raw_input recursion recursive schedule screensaverloopinactive script searchingfile slicenotation sqlite ssh string strings sudokusolver text time tlapse tooltip tuple type ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia wxpython xlib xlwt





