467,927 Members | 1,954 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,927 developers. It's quick & easy.

Read a group of files as a list

Hi!!
I'm a beginner in python and I'm trying to use the files from a
"folder" as a list. What I want to do is read each one as a list, but
just the name of the file, the data inside doesn't matter. How can I do
it? I was trying using a list, but I don't know how "convert" the
elements (files) to a list that I can read and manipulate...

Thanks in advance for your help!!

Citlaly....

Sep 18 '06 #1
  • viewed: 1750
Share:
6 Replies
On 18 Sep 2006 17:09:22 -0700, citlaly <is********@yahoo.com.mxwrote:
Hi!!
I'm a beginner in python and I'm trying to use the files from a
"folder" as a list. What I want to do is read each one as a list, but
just the name of the file, the data inside doesn't matter. How can I do
it? I was trying using a list, but I don't know how "convert" the
elements (files) to a list that I can read and manipulate...

Thanks in advance for your help!!

Citlaly....

--
http://mail.python.org/mailman/listinfo/python-list
Look at the docs for os.path.walk, os.walk, and os.listdir, and pick
the ones that fits your usecase best.
Sep 19 '06 #2
citlaly:
I'm trying to use the files from a
"folder" as a list. What I want to do is read each one as a list, but
just the name of the file, the data inside doesn't matter.
>>import os
os.listdir("...path...")
[.... file names... ]

If you are using Windows I suggest you to invert the slashes of the
path, so instead of:
"C:\WINNT\Fonts"
Use:
"C:/WINNT/Fonts"

Bye,
bearophile

Sep 19 '06 #3
citlaly wrote:
Hi!!
I'm a beginner in python and I'm trying to use the files from a
"folder" as a list. What I want to do is read each one as a list, but
just the name of the file, the data inside doesn't matter. How can I do
it? I was trying using a list, but I don't know how "convert" the
elements (files) to a list that I can read and manipulate...

Thanks in advance for your help!!
C:\Steve\wordpress>dir
Volume in drive C has no label.
Volume Serial Number is 9CA8-2A02

Directory of C:\Steve\wordpress

08/15/2005 12:02 AM <DIR .
08/15/2005 12:02 AM <DIR ..
02/13/2005 04:38 PM 95 index.php
04/01/2003 10:12 AM 15,127 license.txt
05/27/2005 01:51 PM 8,781 readme.html
08/15/2005 12:02 AM <DIR wp-admin
05/09/2005 07:11 AM 1,972 wp-atom.php
05/13/2005 05:07 PM 8,641 wp-blog-header.php
05/03/2005 03:31 AM 2,365 wp-comments-post.php
08/14/2005 02:21 PM 3,369 wp-commentsrss2.php
02/21/2005 02:38 AM 840 wp-config-sample.php
08/15/2005 12:02 AM <DIR wp-content
03/29/2005 11:05 AM 762 wp-feed.php
08/15/2005 12:02 AM <DIR wp-images
08/15/2005 12:02 AM <DIR wp-includes
03/22/2005 05:56 PM 2,319 wp-links-opml.php
06/26/2005 12:27 PM 9,864 wp-login.php
02/01/2005 09:21 AM 5,591 wp-mail.php
03/13/2005 12:36 PM 299 wp-pass.php
02/21/2005 12:19 AM 2,346 wp-rdf.php
04/18/2005 11:35 PM 6,525 wp-register.php
02/21/2005 12:19 AM 1,345 wp-rss.php
02/24/2005 02:29 PM 2,100 wp-rss2.php
08/14/2005 11:57 PM 5,840 wp-settings.php
04/20/2005 06:06 PM 3,069 wp-trackback.php
02/14/2005 12:36 AM 1,922 wp.php
08/14/2005 02:06 PM 38,304 xmlrpc.php
21 File(s) 121,476 bytes
6 Dir(s) 15,239,180,288 bytes free

C:\Steve\wordpress>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
Started with C:/Steve/.pythonrc
>>import glob
glob.glob("*")
['index.php', 'license.txt', 'readme.html', 'wp-admin', 'wp-atom.php',
'wp-blog-header.php', 'wp-comments-post.php', 'wp-commentsrss2.php',
'wp-config-sample.php', 'wp-content', 'wp-feed.php', 'wp-images',
'wp-includes', 'wp-links-opml.php', 'wp-login.php', 'wp-mail.php',
'wp-pass.php', 'wp-rdf.php', 'wp-register.php', 'wp-rss.php',
'wp-rss2.php', 'wp-settings.php', 'wp-trackback.php', 'wp.php',
'xmlrpc.php']

There are os.path functions that will tell you whether a particular file
name (fn) is a directory or a plain file.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 19 '06 #4
Thanks a lot Bearophile!! It works great! Well, it seems something
easy for experts, but for beginners as me, your help is very useful...
thanks!!
Citlalyy
>
>import os
os.listdir("...path...")
[.... file names... ]

If you are using Windows I suggest you to invert the slashes of the
path, so instead of:
"C:\WINNT\Fonts"
Use:
"C:/WINNT/Fonts"

Bye,
bearophile
Sep 19 '06 #5

Thanks Steven! I'll check it!!

Saludos!!

Sep 19 '06 #6

Thanks Steven! I'll check it!!

Saludos!!
Citlalyy

Sep 19 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by junky_fellow | last post: by
10 posts views Thread by just4me | last post: by
5 posts views Thread by th3dude | last post: by
reply views Thread by citlaly | last post: by
8 posts views Thread by andrew.jefferies | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.