Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 29th, 2005, 1:18 PM   #1
disAbled
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 disAbled is on a distinguished road
ftplib help please!

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:


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'

Do I need to update my versions? Seems like it should work, and I'm stumped. 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:


from ftplib import FTP
import os
import re
import sys
import fnmatch

def FTPDown():
	print
	print
	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
	print "Connected to "+ftplocation
	myftp.set_debuglevel(0)
	username=raw_input("Please Enter Username:  ")
	password=raw_input("Please Enter Password:  ")
	print
	print
	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()
	print
	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():
	print
	print
	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
	print "Connected to "+ftplocation
	myftp.set_debuglevel(0)
	username=raw_input("Please Enter Username:  ")
	password=raw_input("Please Enter Password:  ")
	print
	print
	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()
	print
	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"
	print
	print
	print
	print
	print
	print
	print
	print
	print
	print
	print
	menuInput="a"
	while menuInput!="9":
		print
		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():
	print
	print
	target=raw_input("Insert after which item?  ")
	newURL=raw_input("File name for new item?   ")
	linkName=raw_input("Link title for new item?  ")
	print
	print
	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():
	print
	print
	target=raw_input("Delete which item?  ")
	print
	print
	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():
	print
	print
	targetItem=raw_input("Change what text?  ")
	newItem=raw_input("Change to what?  ")
	print
	print
	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"
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
ext=raw_input("--press enter to exit--")
__________________
def EveningChores():
	for beer in fridge:
		myBeer = open(beer)
		while myBeer is not empty:
			Drink(myBeer)
			Belch()
	sleep()
disAbled is offline   Reply With Quote
Old Jun 30th, 2005, 8:05 PM   #2
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 10 Cerulean is on a distinguished road
Quote:
I've been hunting around, and everything seems to indicate that there should be nothing wrong with my code
Very bad assumption to make . Just because you can't see the problem, doesn't mean that it's not there.

Eh... you're passing a method (fileObj.write) to a function that wants a file object as its parameter.
storlines takes a command and a file object, retrlines take a command and a callback. You must have mistaken them somehow.

A little advice: Look at the trackback. It'll show you what you're passing to what function. Cross reference that with the Python documentation, and you can't go wrong.
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC