[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

Walter Prins wprins at gmail.com
Thu Sep 29 18:31:53 CEST 2011


Hi,

On 29 September 2011 17:07, lina <lina.lastname at gmail.com> wrote:

> I found one thing a bit weird, Here is the one:
>
> import os.path
>
> tokens=['E']
> result=[]
>
> """
> for fileName in os.listdir("."):
>     if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
> """
> filedata = open("1.xpm")
> text=filedata.readlines()
> for line in text[23]:
>     print line
>

> 1] $ python try2.py | wc -l
> 206
>

Please go and carefully study how strings, lists etc work -- you don't seem
to grasp what "text[23]" returns vs what text[23:24] etc returns etc.  To
spell it out:  If the variable text refers to a list of strings, then
text[23] is the 24th string.  24th, because the index starts from 0.
text[23:24] by contrast, is a sublist (called a slice) that contains strings
24 and 25 from the original list.

So, if text[23] is already itself a single string, what can the following
code possibly mean?  Eg in general, what does s[0] give you if s is a
string?  A: The first character in s. So then, what does the following do:

for x in text[23]:
  print x

a: It steps through the letters in text[23] and prints each in turn (on a
new line.)

So what you then do when you "wc -l" text[23], is to effectively count the
number of characters in line 24....

You should probably just have run the script without the wc -l and this
would've become very clear very quickly on inspecting the output.   ;)

The other results are similarly explainable, by examining the output. (To be
exact, the lines in the sublists/slices include newline characters.  When
you print them seperately, you therefore end up with more lines than you
think.  Try "print line.strip()" to get rid of the newline.  Try the
following in the python interpreter:

f=open('C:\\Users\\walterp\\Desktop\\1.xpm')
>>> lines=f.readlines()
>>> slice1=lines[23]
>>> slice2=lines[23:24]
>>> slice3=lines[23:25]
>>> print slice1
>>> print slice2
>>> print slice3
>>> print len(slice3)

etc.


Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110929/df5a9f0f/attachment.html>


More information about the Tutor mailing list