#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    9
    Rep 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.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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.

IMN logo majestic logo threadwatch logo seochat tools logo