and i try use module random...

>>> print random.randint(0,10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined

that's my version...

Python 2.5.2 (r252:60911, Aug 1 2008, 00:37:21)
[GCC 4.3.1 20080507 (prerelease) [gcc-4_3-branch revision 135036]] on linux2
Type "help", "copyright", "credits" or "license" for more information.....

Please someone can give me one hand with that? Thanks..

8 Years
Discussion Span
Last Post by Stefano Mtangoo

import random, os
r = random.randint(1,100)
s = "ifconfig wlan0 192.168.%s.%s up" % r

well.. im still with one problem, when i run this script, python give me this answer...

python announce dons't have enough argument.

ok i know im newbie, but this little thing can be a big solution for me.
Thanks FOLKS!


s = "ifconfig wlan0 192.168.%s.%s up" % r

Since your string formatting has two %s, the argument list should also have two values to fill into those spots. So something like this would be more correct:

s = "ifconfig wlan0 192.168.%s.%s up" % ( r, r )

ok.. here im again.....
i got one other problem, how can i write inside other file... for example....
i have one file the name is PARK.conf inside in this file i have this command..

i Want to change the NAME JACK for NAME SARAH... inside to the file PARK.conf how can i do that?


To open a file you use the built-in method open() ; however there are plenty of examples of opening files and reading/writing them on this very site, so I won't waste my time reposting.

Either use the search bar at the top of the site or use your old friend Google. They'll both provide the answers to your question.


Normally when you open a file though it will re-write the file, that is if you open it in write mode. So make sure, if you dont want anything wiped that you can either load the file into a string or something, change what you want and then re-write it to a file. Or you can have a look at append write mode.


fileHandler = open ( 'PARK.conf', 'a' )
fileHandle.write ( 'NAME SARAH' )

ok with that command i write inside to the file PARK.conf NAME SARAH ok but what i want is... read the NAME JACK and REPLACE JACK for SARAH.

i could not figure out that.


Here is a program that does this

import re
pattern = re.compile(r"(NAME\s+)(\w+)")

def replace_names(filename):
  content = open(filename).read()
  content = pattern.sub(my_substitution, content)
  output = open(filename, "w")

def my_substitution(match):
  beforename = match.group(1)
  name = match.group(2)
  print "Replacing name '%s' by 'SARAH'" % name
  return beforename + "SARAH"

if __name__ == "__main__":

  my output -->
Replacing name 'JACK' by 'SARAH'
Replacing name 'Hrothgar' by 'SARAH'

The file PARK.conf was

Hello world
NAME Hrothgar

You can modify the output by changing the function my_substitution so that it returns something different from the name SARAH.


Thank you man!
What about urllib...
i looking for some solution but i can find out...
i can open the website by using urllib2 but it not have modulo seek...
i want to print for example 12th string of one webpage, i try readline() but it just show me the first string....

I love you guy!


Urllib i think gets the source code for a webpage and not what is actually on it. So maybe hunt around for another module that will help you. Or perhaps you actually need the 12th line of the source code of a webpage which you would get like:

>>> import urllib2
>>> data = urllib2.urlopen('http://www.google.com')

The variable data is now a file like object with all of the data that makes up www.google.com.

Hope that helps


Try something like this

f= open("test.txt", "r")
s = f.readlines()
for name in s: 
    name = name.lower()    
    if name == "steve":
        print name
        print "found name %s" %(name, )
        print name
        print "No such Name!"
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.