[Tutor] reading a list of files from a directory (WinXP)
Raymond Hettinger
python@rcn.com
Fri, 31 May 2002 10:03:15 -0400
This is a multi-part message in MIME format.
------=_NextPart_000_0049_01C2088A.618F8560
Content-Type: text/plain;
charset="iso-2022-jp"
Content-Transfer-Encoding: quoted-printable
Try this:
[os.path.splitext(filename)[0] for filename in os.listdir('\\')]
Raymond Hettinger
----- Original Message -----=20
From: Matthew Huggett=20
To: Tutor@python.org=20
Sent: Friday, May 31, 2002 5:59 AM
Subject: [Tutor] reading a list of files from a directory (WinXP)
Hi:
I would like to read a list of files (all with the extension .tfm) =
from a directory, strip off their .tfm extensions, and put them into a =
list (i.e., myList =3D [file1,file2,etc...]).
What would be the most efficient way of doing this?
Do I first need to list the files in a source file-- "C:\ dir /b > =
MyFiles.txt"-- or can I read from the directory directly?
------=_NextPart_000_0049_01C2088A.618F8560
Content-Type: text/html;
charset="iso-2022-jp"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-2022-jp">
<META content=3D"MSHTML 5.50.4207.2601" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Try this:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>[os.path.splitext(filename)[0] for =
filename in=20
os.listdir('\\')]</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Raymond Hettinger</FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV=20
style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
<A title=3Dmhuggett@telusplanet.net=20
href=3D"mailto:mhuggett@telusplanet.net">Matthew Huggett</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3DTutor@python.org =
href=3D"mailto:Tutor@python.org">Tutor@python.org</A> </DIV>
<DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, May 31, 2002 5:59 =
AM</DIV>
<DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Tutor] reading a list =
of files=20
from a directory (WinXP)</DIV>
<DIV><BR></DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2>Hi:</FONT></DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2>I would like to read a list =
of files=20
(all with the extension .tfm) from a directory, strip off their .tfm=20
extensions, and put them into a list (i.e., myList =3D=20
[file1,file2,etc...]).</FONT></DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2>What would be the most =
efficient way of=20
doing this?</FONT></DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2>Do I first need to list the =
files in a=20
source file-- "C:\ dir /b > MyFiles.txt"-- or can I read =
from=20
the directory directly?</FONT></DIV>
<DIV><FONT face=3D"MS UI Gothic" size=3D2></FONT> </DIV>
<DIV><FONT face=3D"MS UI Gothic"=20
size=3D2></FONT> </DIV></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_0049_01C2088A.618F8560--