Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 8th, 2004, 03:16 AM
omit98 omit98 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 9 omit98 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 57 sec
Reputation Power: 0
problem with str()

I want to generate a "random filename".

So:
uniqid = str(os.getpid())+'.'+os.getenv('HOSTNAME')

And I get the error:
UnboundLocalError: local variable 'str' referenced before assignment

Any ideas? Thanks in advance.

Reply With Quote
  #2  
Old April 8th, 2004, 03:57 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
I've never actually heard of an UnboundLocalError, although if this is being raised by one of the functions you're calling it might explain a few things. Can you provide your full program for examination? Also, what version of Python and on what OS are you running?

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old April 8th, 2004, 04:08 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
I suspect that the environment variable 'HOSTNAME' is not defined, in which case os.getenv will return None.

With my version of Python 2.3.3 it correctly reports that you can't concatenate None to a string. What version are you using?

There are many ways to generate a more random file name (the tmpfile module could be useful).
How about this:
uniqid = str(os.getpid())+'.'+str(time.time())[-6:]

as long as you don't request another filename within 100 ms in the same process.
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #4  
Old April 8th, 2004, 05:31 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,188 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 11 h 25 m 43 sec
Reputation Power: 251
Quote:
Originally Posted by omit98
I want to generate a "random filename".

So:
uniqid = str(os.getpid())+'.'+os.getenv('HOSTNAME')

And I get the error:
UnboundLocalError: local variable 'str' referenced before assignment

Any ideas? Thanks in advance.


This error is caused because Python thinks 'str' is a local variable. This probably means that you use the name later in the same function to hold a value. e.g.

Code:
>>> def test():
... 	str(23)
... 	str = 'foo'
... 	
>>> test()
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
  File "<interactive input>", line 2, in test
UnboundLocalError: local variable 'str' referenced before assignment
>>> 


Because the code assigns to 'str' the Python compiler assumes that it is a local variable, but you can't use a local variable before you have assigned it a value, hence the exception. The solution is to search for occurrences of 'str' in your code, and if it is being used as a variable name then rename the variable.

Dave - The Developers' Coach

Last edited by DevCoach : April 8th, 2004 at 05:34 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > problem with str()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway