•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,165 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,977 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 4336 | Replies: 1
![]() |
•
•
Join Date: Jun 2005
Posts: 2
Reputation:
Rep Power: 0
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
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:
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--")---
so long and thanks for all the fish
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
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 / 2003)
Other Threads in the Python Forum
- Previous Thread: my pride and joy
- Next Thread: Regular Exp


Linear Mode