472,804 Members | 875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

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
6 1931
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
0
by: Bennett Haselton | last post by:
I'm trying to port my MySQL tables for a database called "tracerlock" from one server to another. On the old server, in the /var/lib/mysql/tracerlock directory, there was a ".MYD", ".MYI" and...
2
by: Raghu Gupta | last post by:
Hi, Actually i am facing some problem using quickbooks and presently i am not so expert in using it. I am using c# code to retrive the data from quickbooks. it doesn't matter which language we...
1
by: Sandeep | last post by:
Hi, I want to read & print a UTF-8 encoded characters stored in a file. I want to write a C program on Linux platform to achieve this. I am new to UTF-8. I will appreciate any sample...
3
by: junky_fellow | last post by:
Consider a scenario, where one process uses mmap() to access/modify a particaular file and at the same time other process uses read/write to access/modify the same file. I wanted to know whether...
10
by: just4me | last post by:
Exactly how do I read a specific location in the bios eprom. (such as debug.exe location ffff:0000)
5
by: th3dude | last post by:
I've searched quite a bit for this one and i can't find any solid solution for what i'm doing here. Right now i'm geting an xml string from an API, creating an xml file, then read the file with...
0
by: citlaly | last post by:
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...
8
by: andrew.jefferies | last post by:
Hi, I'm trying to write a simple log parsing program. I noticed that it isn't reading my log file to the end. My log is around 200,000 lines but it is stopping at line 26,428. I checked that...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.