>>> import random
>>> print random.randint(0, 10)
Also read a python tutorial !
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
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 )
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
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.
jlm699
Veteran Poster
1,112 posts since Jul 2008
Reputation Points: 355
Solved Threads: 292
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.
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
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.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
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
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
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()
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392