[Tutor] glob in order of the file numbers
Emad Nawfal (عماد نوفل)
emadnawfal at gmail.com
Sat Mar 7 13:36:23 CET 2009
On Fri, Mar 6, 2009 at 11:34 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Fri, Mar 6, 2009 at 9:52 PM, Emad Nawfal (عماد نوفل)
> <emadnawfal at gmail.com> wrote:
> > Hi Tutors,
> > suppose I have four files in the current directory: 1.temp, 2.temp,
> 3.temp,
> > and 4.temp. I want to use glob, or anything else, to print the contents
> of
> > the files in their respective orders, where the content of 1.temp gets
> > printed, then 2.temp, then 3.temp, then 4.temp.
> > I write the following, but it does not get me what I want:
> >
> > import glob
> > for path in glob.iglob("*.temp"):
> > infile = open(path)
> > for line in infile:
> > print line.strip()
>
> Since you know the file names, just build the names directly:
>
> for i in range(1, 5):
> path = '%s.temp' % i
> print open(path).read()
>
> Kent
>
Thank you all for the solutions. I had figured out Kent's solution before,
so thank you for letting me know that I did it right.
So, glob just creates a list of the file names.
Thank you all again.
--
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"
Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090307/18189095/attachment.htm>
More information about the Tutor
mailing list