473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1967
On 18 Sep 2006 17:09:22 -0700, citlaly <is********@yah oo.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\wordpr ess>dir
Volume in drive C has no label.
Volume Serial Number is 9CA8-2A02

Directory of C:\Steve\wordpr ess

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.ph p
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\wordpr ess>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.ph p',
'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
14153
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 Smarty-2.6.7.tar.gz on a system running WindowsXP SP2. Apache and PHP tested out fine. After adding Smarty, I ran the following http://localhost/testphp.php
0
2748
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 ".frm" file for every table in the database. So after creating a database called "tracerlock" on the new server, I copied these files over to the...
2
8297
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 are using. So i have .iif file of employee list,so through code i have to read .iif file and get the data out of it and store that data in database....
1
4941
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 programs OR guidance on this. Thanks in advance,
3
2303
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 it may cause inconsistency or not? May be this question is out of context for c.l.c, i would be grateful if someone help me. Just a hint will do. ...
10
3071
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
9086
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 XPath or XmlReader, grab the attribute data, dump it into the database. Once all that's done i blow away all the xml files and start the whole...
0
841
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 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... ...
8
2143
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 line and there aren't any special characters. This is the file reading code segment that I'm using: sysFile=open(sysFilename,'r')
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.