From my other post I am making a simple program that creates an RSS feed with Python. Now when I run my program so far, I get errors. It says "something is not defined". The word something is replaced by the name of the function I'm trying to use. my article function when ran, comes up with "article is not defined" etc.
Here's my code: What is wrong with it? - #!/usr/bin/env python
-
import ftplib
-
import getpass
-
-
def mainfeed():
-
name=raw_input("Name of Feed: ")
-
desc=raw_input("Description of Feed: ")
-
url=raw_input("Location of Website: ")
-
again()
-
-
def again():
-
again=raw_input("Would you like to add an article to the feed? Y/N: ")
-
if again == "Y":
-
article()
-
elif again =="N":
-
writeb()
-
else:
-
print again + """is not a valid option. You must select either 'Y' or 'N'"""
-
fail()
-
-
def fail():
-
again()
-
-
def article():
-
aname=raw_input("Name of Article: ")
-
adesc=raw_input("Description of Article: ")
-
aurl=raw_input("Location of Article: ")
-
finish()
-
-
def finish():
-
print "Saving parameters to rss-feed.xml...please wait..."
-
print "Finished"
-
writea()
-
-
def writea():
-
file_name = "rss-feed.xml"
-
f = open(file_name, 'w')
-
outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
outlist.append(' <link>%s</link>' % url)
-
outlist.append(' <description>%s</description>' % desc)
-
outlist.append(' <item>\n <title>%s</title>' % aname)
-
outlist.append(' <link>%s</link>' % aurl)
-
outlist.append(' <description>%s</description>' % adesc)
-
outlist.append(' </item>')
-
outlist.append(' </channel>')
-
outlist.append('</rss>')
-
f.write('\n'.join(outlist))
-
f.close()
-
ftpqe()
-
-
def writeb():
-
file_name = "rss-feed.xml"
-
f = open(file_name, 'w')
-
outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
outlist.append(' <link>%s</link>' % url)
-
outlist.append(' <description>%s</description>' % desc)
-
outlist.append(' </channel>')
-
outlist.append('</rss>')
-
f.write('\n'.join(outlist))
-
f.close()
-
ftpqe()
-
-
def ftpqe():
-
ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
-
if ftpq == "Y":
-
ftp()
-
elif ftpq =="N":
-
end()
-
else:
-
print again + """is not a valid option. You must select either 'Y' or 'N'"""
-
fail2()
-
-
def fail2():
-
ftpqe()
-
-
def ftp():
-
server=raw_input("Server name: ")
-
login=raw_input("Enter your login: ")
-
password = getpass.unix_getpass("Enter your password: ")
-
ftplib.FTP(server,login,password)
-
f = open('rss-feed.xml','rb')
-
s.storbinary('STOR rss-feed.xml', f)
-
f.close()
-
s.quit()
-
print "Your file has been uploaded"
-
end()
-
-
def end():
-
print "Thank you"
-
-
mainfeed()
10 2384 bvdet 2,851
Recognized Expert Moderator Specialist
In this line: - s.storbinary('STOR rss-feed.xml', f)
I don't see where s is defined. It would be helpful if you would post the exact error message and include all tracebacks. If you are not seeing this information, try enclosing your code in a try/except block similiar to this: - import sys, traceback
-
-
def formatExceptionInfo(level=6):
-
error_type, error_value, trbk = sys.exc_info()
-
info_list = ["Error: %s \nDescription: %s \nTraceback:\n" % (error_type.__name__, error_value), ]
-
info_list.extend(traceback.format_tb(trbk, level))
-
return ''.join(info_list)
-
-
try:
-
mainfeed()
-
except:
-
print formatExceptionInfo()
Here are the errors I get so far:
No 1: - Name of Feed: test
-
Description of Feed: test
-
Location of Website: test
-
Would you like to add an article to the feed? Y/N: N
-
Traceback (most recent call last):
-
File "rssgps.py", line 93, in <module>
-
mainfeed()
-
File "rssgps.py", line 9, in mainfeed
-
again()
-
File "rssgps.py", line 16, in again
-
writeb()
-
File "rssgps.py", line 56, in writeb
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
NameError: global name 'name' is not defined
-
no 2 -
Name of Feed: test
-
Description of Feed: test
-
Location of Website: test
-
Would you like to add an article to the feed? Y/N: Y
-
Name of Article: test
-
Description of Article: test
-
Location of Article: test
-
Saving parameters to rss-feed.xml...please wait...
-
Finished
-
Traceback (most recent call last):
-
File "rssgps.py", line 93, in <module>
-
mainfeed()
-
File "rssgps.py", line 9, in mainfeed
-
again()
-
File "rssgps.py", line 14, in again
-
article()
-
File "rssgps.py", line 28, in article
-
finish()
-
File "rssgps.py", line 33, in finish
-
writea()
-
File "rssgps.py", line 39, in writea
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
NameError: global name 'name' is not defined
-
-
I have more on the functions that are executed later on in the script, but this is so far.
But name IS defined, from the raw input at the start of the mainfeed function!
bvdet 2,851
Recognized Expert Moderator Specialist
It is a matter of scope and the way Python resolves names. When a function executes, a new namespace is created which includes the names of the function parameters and variables assigned in the function. When resolving names, the interpreter first checks the local namespace, in this case, the namespace created by the function. If no match is found, it checks the global namespace. The global namespace is always the module which defines the function. If no match is found, it checks the built-in namespace before raising a NameError exception. As you can see, the interpreter never checks the namespaces of the other functions in your module. You must make the variable names global or pass arguments to your functions.
@bvdet
Thanks! I'll make them global now, and then I'll post the other errors later. That's one bit solved.
-
#!/usr/bin/env python
-
import ftplib
-
import getpass
-
-
name=raw_input("Name of Feed: ")
-
desc=raw_input("Description of Feed: ")
-
url=raw_input("Location of Website: ")
-
print "Your main feeds settings have been updated"
-
print "Now information about adding an article to the feed.Other articles can be added manually later"
-
aname=raw_input("Name of Article: ")
-
adesc=raw_input("Description of Article: ")
-
aurl=raw_input("Location of Article: ")
-
finish()
-
-
def finish():
-
print "Saving parameters to rss-feed.xml...please wait..."
-
print "Finished"
-
writea()
-
-
def writea():
-
file_name = "rss-feed.xml"
-
f = open(file_name, 'w')
-
outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
outlist.append(' <link>%s</link>' % url)
-
outlist.append(' <description>%s</description>' % desc)
-
outlist.append(' <item>\n <title>%s</title>' % aname)
-
outlist.append(' <link>%s</link>' % aurl)
-
outlist.append(' <description>%s</description>' % adesc)
-
outlist.append(' </item>')
-
outlist.append(' </channel>')
-
outlist.append('</rss>')
-
f.write('\n'.join(outlist))
-
f.close()
-
ftpqe()
-
-
def writeb():
-
file_name = "rss-feed.xml"
-
f = open(file_name, 'w')
-
outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
-
outlist.append(' <channel>\n <title>%s</title>' % name)
-
outlist.append(' <link>%s</link>' % url)
-
outlist.append(' <description>%s</description>' % desc)
-
outlist.append(' </channel>')
-
outlist.append('</rss>')
-
f.write('\n'.join(outlist))
-
f.close()
-
ftpqe()
-
-
def ftpqe():
-
ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
-
if ftpq == "Y":
-
ftp()
-
elif ftpq =="N":
-
end()
-
else:
-
print again + """is not a valid option. You must select either 'Y' or 'N'"""
-
fail2()
-
-
def fail2():
-
ftpqe()
-
-
def ftp():
-
server=raw_input("Server name: ")
-
login=raw_input("Enter your login: ")
-
password = getpass.unix_getpass("Enter your password: ")
-
ftplib.FTP(server,login,password)
-
f = open('rss-feed.xml','rb')
-
s.storbinary('STOR rss-feed.xml', f)
-
f.close()
-
s.quit()
-
print "Your file has been uploaded"
-
end()
-
-
def end():
-
print "Thank you for using "
-
I changed it to this now, the names and article names have been made global, and now I get this error. It's this I don't understand the most, because finish is obviously a function. error: -
Traceback (most recent call last):
-
File "rssgps.py", line 13, in <module>
-
finish()
-
NameError: name 'finish' is not defined
-
-
Do I misunderstand functions?
bvdet 2,851
Recognized Expert Moderator Specialist
You have to define it before you call it. -
def finish():
-
....do stuff....
-
-
finish()
Thanks once again, you've really helped me. Now 1 final question.
Everything works apart from the FTP. This is the error: -
Do you want to upload your XML file to your website via FTP? Y/N: Y
-
Server name: test
-
Enter your login: test
-
Enter your password:
-
Traceback (most recent call last):
-
File "rssgps.py", line 76, in <module>
-
finish()
-
File "rssgps.py", line 8, in finish
-
writea()
-
File "rssgps.py", line 25, in writea
-
ftpqe()
-
File "rssgps.py", line 43, in ftpqe
-
ftp()
-
File "rssgps.py", line 57, in ftp
-
ftplib.FTP(server,login,password)
-
File "/usr/lib/python2.5/ftplib.py", line 107, in __init__
-
self.connect(host)
-
File "/usr/lib/python2.5/ftplib.py", line 117, in connect
-
for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
-
socket.gaierror: (-2, 'Name or service not known')
-
-
Any ideas? Thanks
bvdet 2,851
Recognized Expert Moderator Specialist
You did not assign the ftplib.FTP object to a variable. Try this: -
s = ftplib.FTP(server,login,password)
-
f = open('rss-feed.xml','rb')
-
s.storbinary('STOR rss-feed.xml', f)
-
f.close()
-
s.quit()
If that is not the problem, you may have an incorrect server name or user name. This worked for me on my FTP server: - >>> import ftplib
-
>>> f = ftplib.FTP('ftp.bvdetailing.com', '*********', '********')
-
>>> f.mkd('ftplib_test')
-
'ftplib_test'
-
>>> f.close()
-
>>>
Yet again, thank you. I can only keep thanking you!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Amy |
last post by:
This is what I want to do:
1. Delete all tables in database with table names that ends with a
number.
2. Leave all other tables in tact.
3. Table names are unknown.
4. Numbers attached to table names are unknown.
5. Unknown number of tables in database.
For example:
|
by: Spartanicus |
last post by:
The document at http://homepage.ntlworld.com/spartanicus/custom_dtd.htm
uses a custom DTD, the w3c validator validates it but with this warning:
"Unknown Parse Mode!
The MIME Media Type (text/html) for this document is used to serve both
SGML and XML based documents, and it is not possible to disambiguate it
based on the DOCTYPE Declaration in your document. Parsing will continue
in SGML mode."...
|
by: Torrent |
last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown function."
It was annoying because I had checked created the whole document and tested it in Internet Explorer 6.0 and everything worked perfectly; no errors or warnings.
After doing research i found out that most often times the error that i was...
|
by: quilkin |
last post by:
I am getting a System.Data.SqlClient.SqlException with a message string of
'Unknown Error'. Happens occasionally when creating a "new SqlCommand(...)"
while the same command works perfectly happily hundreds of other times. A
side effect is that the connection closes.
..NET version is 1.1, same thing happens on various machines, but seems to
happen more often on slower ones.
Any ideas?
|
by: Shaun |
last post by:
Hi all,
I'm trying to implement a custom session handler that writes session
data to a MySQL database. It works fine about 99% of the time. Trouble
is, at random intervals, I get entries like the following in my
php-error.log:
PHP Warning: Unknown: A session is active. You
cannot change the session module's ini settings at this time. in
Unknown on line 0
| |
by: Peter Afonin |
last post by:
Hello,
I'm developing an Atlas application in ASP.NET 2.0. Everything was OK, but
suddenly I started to get some strange errors on one of the pages:
When I click any button or invoke any server-side script, I'm getting a
popup window: "Unknown error".
So I'm stuck. There is no error number, so I cannot investigate it further.
There are no errors on the server side - when I debug, everything is OK, the
|
by: fjanon |
last post by:
Hi,
I went through the HTML spec without finding the description of what
the browsers behavior should/must be regarding tags or attributes they
don't understand. I did a quick experiment like this:
<mytag style='display:none'>
<h3>Hello</h3>
|
by: S.T |
last post by:
Hi!
I've spend this weekend trying and coding a php-based atom- and
rss-feed writer. I don't know if this is the right place for my
questions. However, since both feeds are based on xml, maybe someone
can help.
I understand that I have to replace all unknown entities, such as
"ä". I wonder what's the common way to do so. For XML-Documents,
I'd prefer to make use of an external DTD, say, referring to
|
by: Calvin Cheng |
last post by:
Hi,
I am attempting to convert a bunch of .txt files into html using the
docutils package.
It works for most of the txt files except for the index.txt file which
gives 2 errors:
(1) <Error/3Unknown Directive type "toctree"
(2) (ERROR/3) Unknown interpreted text role "ref".
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |