Variable + String Format

C

Chris Rebert

Hi all,

I have this piece of code:
#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')

Why are you opening the file in binary mode? It's content is text!
def readLines():

for line in file.read():

..read() reads the *entire* file into a string. When you iterate over a
string, such as in a for-loop, you get *individual characters*,
whereas you appear to want lines of text. To go line-by-line, use `for
line in file` instead (note: `line` will include the trailing newline
at the end of each line).
And don't use `file` as a variable name; it shadows the name of the
buitlin type.

Cheers,
Chris
 
J

John Machin

Why are you opening the file in binary mode? It's content is text!





.read() reads the *entire* file into a string. When you iterate over a
string, such as in a for-loop, you get *individual characters*,
whereas you appear to want lines of text. To go line-by-line, use `for
line in file` instead (note: `line` will include the trailing newline
at the end of each line).
And don't use `file` as a variable name; it shadows the name of the
buitlin type.

Changed as suggested above, with further comments:

wordList = "/tmp/Wordlist"
f = open(wordList, 'r')
def readLines():
## There is no point in putting the next four lines
## inside a function if you are going to call the
## function (a) immediately and (b) once only.
## All it does is provide scope for unwanted complications.
for line in f:
line = line.rstrip('\n')
print line + '.com '
## Complications like this next line; why is it returning "line"??
## And why is it indented like that?
## It will cause only the first line to be read.
return line
## Maybe you you meant it to be here:
return
## But then it's redundant because a function returns anyway
## when flow of control would fall off the bottom.
readLines()
f.close()

HTH,
John
 
G

Gabriel Genellina

#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##########################################################################

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line
e.g. test would only print t and readline() does the same thing.

This should print every line in the file, adding .com at the end:

wordList = "/tmp/Wordlist"
with open(wordList, 'r') as wl:
for line in wl:
print line.rstrip() + '.com '

Note that:
- I iterate over the file self; files are their own line-iterators.
- I've used the 'r' mode instead (I assume it's a text file because you
read it line by line, and as you don't update it, the '+' isn't required)
- the line read includes the end-of-line '\n' at the end; rstrip() removes
it and any trailing whitespace. If you don't want this, use rstrip('\n')
- I've used the with statement to ensure the file is closed at the end
 
G

Gabriel Genellina

#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##########################################################################

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line
e.g. test would only print t and readline() does the same thing.

This should print every line in the file, adding .com at the end:

wordList = "/tmp/Wordlist"
with open(wordList, 'r') as wl:
for line in wl:
print line.rstrip() + '.com '

Note that:
- I iterate over the file self; files are their own line-iterators.
- I've used the 'r' mode instead (I assume it's a text file because you
read it line by line, and as you don't update it, the '+' isn't required)
- the line read includes the end-of-line '\n' at the end; rstrip() removes
it and any trailing whitespace. If you don't want this, use rstrip('\n')
- I've used the with statement to ensure the file is closed at the end
 
S

Steven D'Aprano

Thanks for the quick response guys. Help me out a alot. I'm a newbie to
python and your replies help me understand a bit more about python!!

Joel, unless you have Guido's time machine and are actually posting from
the future, the clock, or possibly the time zone, on your PC is set
wrong.
 
J

Joel Ross

Hi all,

I have this piece of code:
#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##########################################################################

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line
e.g. test would only print t and readline() does the same thing.

I have also tried readlines() which gives me the result:

test
..com

The file Wordlist has one word on each line for example

test
test1
test2

I need it to loop though the Wordlist file and print/return the results

test.com
test1.com
test2.com

I'm just looking for a point in the right direction.

Much Thanks

JOelC
 
J

Joel Ross

Joel said:
Hi all,

I have this piece of code:
#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##########################################################################

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line
e.g. test would only print t and readline() does the same thing.

I have also tried readlines() which gives me the result:

test
.com

The file Wordlist has one word on each line for example

test
test1
test2

I need it to loop though the Wordlist file and print/return the results

test.com
test1.com
test2.com

I'm just looking for a point in the right direction.

Much Thanks

JOelC


THAT'S BETTER!!! :)
 
J

Joel Ross

Joel said:
Hi all,

I have this piece of code:
#########################################################################

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##########################################################################

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line
e.g. test would only print t and readline() does the same thing.

I have also tried readlines() which gives me the result:

test
.com

The file Wordlist has one word on each line for example

test
test1
test2

I need it to loop though the Wordlist file and print/return the results

test.com
test1.com
test2.com

I'm just looking for a point in the right direction.

Much Thanks

JOelC


Thanks for the quick response guys. Help me out a alot. I'm a newbie to
python and your replies help me understand a bit more about python!!

Thank You
 
J

Joel Ross

Steven said:
Joel, unless you have Guido's time machine and are actually posting from
the future, the clock, or possibly the time zone, on your PC is set
wrong.
I'm from the distant future. Too protect and Serve humankind!!!

lol thanks dude. I'm a day ahead of myself. I had my linux box running
off UTC and it didn't include daylight savings so I adjusted it myself,
must have accidentally changed the day as well :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top