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..

Recommended Answers

All 13 Replies

>>> import random
>>> print random.randint(0, 10)

Also read a python tutorial !

#!/usr/bin/python
import random, os
r = random.randint(1,100)
s = "ifconfig wlan0 192.168.%s.%s up" % r
os.system(s)

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

#./d.py
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 )

now is working! Thanks!

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..
NAME JACK

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' )
fileHandle.close()

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")
  output.write(content)
  output.close()

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__":
  replace_names("PARK.conf")

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

The file PARK.conf was

Hello world
NAME JACK
....
NAME Hrothgar
etc
...

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....

Thanks!
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, )
    else:
        print name
        print "No such Name!"
        
f.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.