473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unknown errors, RSS feed

11
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?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. import ftplib
  3. import getpass
  4.  
  5. def mainfeed():
  6.     name=raw_input("Name of Feed: ")
  7.     desc=raw_input("Description of Feed: ")
  8.     url=raw_input("Location of Website: ")
  9.     again()
  10.  
  11. def again():
  12.     again=raw_input("Would you like to add an article to the feed? Y/N: ")
  13.     if again == "Y":
  14.         article()
  15.     elif again =="N":
  16.         writeb()
  17.     else:
  18.         print again + """is not a valid option. You must select either 'Y' or 'N'"""
  19.         fail()
  20.  
  21. def fail():
  22.     again()
  23.  
  24. def article():
  25.     aname=raw_input("Name of Article: ")
  26.     adesc=raw_input("Description of Article: ")
  27.     aurl=raw_input("Location of Article: ")
  28.     finish()
  29.  
  30. def finish():
  31.     print "Saving parameters to rss-feed.xml...please wait..."
  32.     print "Finished"   
  33.     writea()
  34.  
  35. def writea():
  36.     file_name = "rss-feed.xml"
  37.     f = open(file_name, 'w')
  38.     outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
  39.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  40.     outlist.append('    <link>%s</link>' % url)
  41.     outlist.append('    <description>%s</description>' % desc)
  42.     outlist.append('    <item>\n        <title>%s</title>' % aname)
  43.     outlist.append('        <link>%s</link>' % aurl)
  44.     outlist.append('        <description>%s</description>' % adesc)
  45.     outlist.append('    </item>')
  46.     outlist.append('  </channel>')
  47.     outlist.append('</rss>')  
  48.     f.write('\n'.join(outlist))
  49.     f.close()
  50.     ftpqe()
  51.  
  52. def writeb():
  53.     file_name = "rss-feed.xml"
  54.     f = open(file_name, 'w')
  55.     outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
  56.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  57.     outlist.append('    <link>%s</link>' % url)
  58.     outlist.append('    <description>%s</description>' % desc)
  59.     outlist.append('  </channel>')
  60.     outlist.append('</rss>')  
  61.     f.write('\n'.join(outlist))
  62.     f.close()
  63.     ftpqe()
  64.  
  65. def ftpqe():
  66.     ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
  67.     if ftpq == "Y":
  68.         ftp()
  69.     elif ftpq =="N":
  70.         end()
  71.     else:
  72.         print again + """is not a valid option. You must select either 'Y' or 'N'"""
  73.         fail2()
  74.  
  75. def fail2():
  76.     ftpqe()
  77.  
  78. def ftp():
  79.     server=raw_input("Server name: ")
  80.     login=raw_input("Enter your login: ")
  81.     password = getpass.unix_getpass("Enter your password: ")
  82.     ftplib.FTP(server,login,password)
  83.     f = open('rss-feed.xml','rb') 
  84.     s.storbinary('STOR rss-feed.xml', f)
  85.     f.close()
  86.     s.quit()
  87.     print "Your file has been uploaded"
  88.     end()
  89.  
  90. def end():
  91.     print "Thank you"
  92.  
  93. mainfeed()
Dec 24 '08 #1
10 2340
bvdet
2,851 Expert Mod 2GB
In this line:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. import sys, traceback
  2.  
  3. def formatExceptionInfo(level=6):
  4.     error_type, error_value, trbk = sys.exc_info()
  5.     info_list = ["Error: %s \nDescription: %s \nTraceback:\n" % (error_type.__name__, error_value), ]
  6.     info_list.extend(traceback.format_tb(trbk, level))
  7.     return ''.join(info_list)
  8.  
  9. try:
  10.     mainfeed()
  11. except:
  12.     print formatExceptionInfo()
Dec 24 '08 #2
bhass
11
Here are the errors I get so far:

No 1:
Expand|Select|Wrap|Line Numbers
  1. Name of Feed: test
  2. Description of Feed: test
  3. Location of Website: test
  4. Would you like to add an article to the feed? Y/N: N
  5. Traceback (most recent call last):
  6.   File "rssgps.py", line 93, in <module>
  7.     mainfeed()
  8.   File "rssgps.py", line 9, in mainfeed
  9.     again()
  10.   File "rssgps.py", line 16, in again
  11.     writeb()
  12.   File "rssgps.py", line 56, in writeb
  13.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  14. NameError: global name 'name' is not defined
  15.  
no 2
Expand|Select|Wrap|Line Numbers
  1. Name of Feed: test
  2. Description of Feed: test
  3. Location of Website: test
  4. Would you like to add an article to the feed? Y/N: Y
  5. Name of Article: test
  6. Description of Article: test
  7. Location of Article: test
  8. Saving parameters to rss-feed.xml...please wait...
  9. Finished
  10. Traceback (most recent call last):
  11.   File "rssgps.py", line 93, in <module>
  12.     mainfeed()
  13.   File "rssgps.py", line 9, in mainfeed
  14.     again()
  15.   File "rssgps.py", line 14, in again
  16.     article()
  17.   File "rssgps.py", line 28, in article
  18.     finish()
  19.   File "rssgps.py", line 33, in finish
  20.     writea()
  21.   File "rssgps.py", line 39, in writea
  22.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  23. NameError: global name 'name' is not defined
  24.  
  25.  
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!
Dec 24 '08 #3
bvdet
2,851 Expert Mod 2GB
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.
Dec 24 '08 #4
bhass
11
@bvdet
Thanks! I'll make them global now, and then I'll post the other errors later. That's one bit solved.
Dec 24 '08 #5
bhass
11
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. import ftplib
  3. import getpass
  4.  
  5. name=raw_input("Name of Feed: ")
  6. desc=raw_input("Description of Feed: ")
  7. url=raw_input("Location of Website: ")
  8. print "Your main feeds settings have been updated"
  9. print "Now information about adding an article to the feed.Other articles can be added manually later"
  10. aname=raw_input("Name of Article: ")
  11. adesc=raw_input("Description of Article: ")
  12. aurl=raw_input("Location of Article: ")
  13. finish()
  14.  
  15. def finish():
  16.     print "Saving parameters to rss-feed.xml...please wait..."
  17.     print "Finished"   
  18.     writea()
  19.  
  20. def writea():
  21.     file_name = "rss-feed.xml"
  22.     f = open(file_name, 'w')
  23.     outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
  24.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  25.     outlist.append('    <link>%s</link>' % url)
  26.     outlist.append('    <description>%s</description>' % desc)
  27.     outlist.append('    <item>\n        <title>%s</title>' % aname)
  28.     outlist.append('        <link>%s</link>' % aurl)
  29.     outlist.append('        <description>%s</description>' % adesc)
  30.     outlist.append('    </item>')
  31.     outlist.append('  </channel>')
  32.     outlist.append('</rss>')  
  33.     f.write('\n'.join(outlist))
  34.     f.close()
  35.     ftpqe()
  36.  
  37. def writeb():
  38.     file_name = "rss-feed.xml"
  39.     f = open(file_name, 'w')
  40.     outlist = ['<?xml version="1.0" encoding="ISO-8859-1" ?>', '<rss version="0.91">',]
  41.     outlist.append('  <channel>\n    <title>%s</title>' % name)
  42.     outlist.append('    <link>%s</link>' % url)
  43.     outlist.append('    <description>%s</description>' % desc)
  44.     outlist.append('  </channel>')
  45.     outlist.append('</rss>')  
  46.     f.write('\n'.join(outlist))
  47.     f.close()
  48.     ftpqe()
  49.  
  50. def ftpqe():
  51.     ftpq=raw_input("Do you want to upload your XML file to your website via FTP? Y/N: ")
  52.     if ftpq == "Y":
  53.         ftp()
  54.     elif ftpq =="N":
  55.         end()
  56.     else:
  57.         print again + """is not a valid option. You must select either 'Y' or 'N'"""
  58.         fail2()
  59.  
  60. def fail2():
  61.     ftpqe()
  62.  
  63. def ftp():
  64.     server=raw_input("Server name: ")
  65.     login=raw_input("Enter your login: ")
  66.     password = getpass.unix_getpass("Enter your password: ")
  67.     ftplib.FTP(server,login,password)
  68.     f = open('rss-feed.xml','rb') 
  69.     s.storbinary('STOR rss-feed.xml', f)
  70.     f.close()
  71.     s.quit()
  72.     print "Your file has been uploaded"
  73.     end()
  74.  
  75. def end():
  76.     print "Thank you for using "
  77.  
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:

Expand|Select|Wrap|Line Numbers
  1. Traceback (most recent call last):
  2.   File "rssgps.py", line 13, in <module>
  3.     finish()
  4. NameError: name 'finish' is not defined
  5.  
  6.  
Do I misunderstand functions?
Dec 24 '08 #6
bvdet
2,851 Expert Mod 2GB
You have to define it before you call it.
Expand|Select|Wrap|Line Numbers
  1. def finish():
  2.     ....do stuff....
  3.  
  4. finish()
Dec 24 '08 #7
bhass
11
Thanks once again, you've really helped me. Now 1 final question.
Everything works apart from the FTP. This is the error:
Expand|Select|Wrap|Line Numbers
  1. Do you want to upload your XML file to your website via FTP? Y/N: Y
  2. Server name: test
  3. Enter your login: test
  4. Enter your password: 
  5. Traceback (most recent call last):
  6.   File "rssgps.py", line 76, in <module>
  7.     finish()
  8.   File "rssgps.py", line 8, in finish
  9.     writea()
  10.   File "rssgps.py", line 25, in writea
  11.     ftpqe()
  12.   File "rssgps.py", line 43, in ftpqe
  13.     ftp()
  14.   File "rssgps.py", line 57, in ftp
  15.     ftplib.FTP(server,login,password)
  16.   File "/usr/lib/python2.5/ftplib.py", line 107, in __init__
  17.     self.connect(host)
  18.   File "/usr/lib/python2.5/ftplib.py", line 117, in connect
  19.     for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
  20. socket.gaierror: (-2, 'Name or service not known')
  21.  
  22.  
Any ideas? Thanks
Dec 24 '08 #8
bvdet
2,851 Expert Mod 2GB
You did not assign the ftplib.FTP object to a variable. Try this:
Expand|Select|Wrap|Line Numbers
  1.     s = ftplib.FTP(server,login,password)
  2.     f = open('rss-feed.xml','rb') 
  3.     s.storbinary('STOR rss-feed.xml', f)
  4.     f.close()
  5.     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:
Expand|Select|Wrap|Line Numbers
  1. >>> import ftplib
  2. >>> f = ftplib.FTP('ftp.bvdetailing.com', '*********', '********')
  3. >>> f.mkd('ftplib_test')
  4. 'ftplib_test'
  5. >>> f.close()
  6. >>> 
Dec 24 '08 #9
bhass
11
Yet again, thank you. I can only keep thanking you!
Dec 24 '08 #10
bvdet
2,851 Expert Mod 2GB
You're welcome. You did a good job of defining your problem and seeing the thread through. Fortunately, I was able to help.

-BV
Dec 24 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
8
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...
3
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...
6
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...
0
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...
5
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...
5
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...
0
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...
2
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)...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.