473,651 Members | 3,012 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parse text files in a directory?

hi everybody
im a newbie in python, i have a question

how do u parse a bunch of text files in a directory?

directory: /dir
files: H20080101.txt ,
H20080102.txt,H 20080103.txt,H2 0080104.txt,H20 080105.txt etc......

i already got a python script to read and insert a single text files
into a postgres db.

is there anyway i can do it in a batch, cause i got like 2000 txt
files.

thanks in advance

joe
Jan 2 '08 #1
1 2205
jo3c wrote:
hi everybody
im a newbie in python, i have a question

how do u parse a bunch of text files in a directory?

directory: /dir
files: H20080101.txt ,
H20080102.txt,H 20080103.txt,H2 0080104.txt,H20 080105.txt etc......

i already got a python script to read and insert a single text files
into a postgres db.

is there anyway i can do it in a batch, cause i got like 2000 txt
files.
>>import os
for filename in os.path.listdir (directory):
.... if is_interesting( filename):
.... do_something(fi lename)

you'll have to implement is_interesting( ) to catch filenames
you're interested in and you've already implemented the
do_something() body.

You might want to look at the "glob" module which allows for easy
filespec testing and name expansion for your "H*.txt" format:

for filename in glob.glob('H*.t xt'):
do_something(fi lename)

If they're in subdirectories, you may want to investigate the
os.walk() generator which will walk the directory tree and allow
you to do something with the files:

for path, dirs, files in os.walk(directo ry):
for filename in files:
filename = os.sep.join(pat h, filename)
if is_interesting( filename):
do_something(fi lename)

-tkc


Jan 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5173
by: Els | last post by:
***newbie question*** Hi, I am trying to make my server (Apache) parse .html files as .php. I found this line of code: ForceType application/x-httpd-php placed it in an .htaccess file and uploaded it to the directory I wanted it to work. And it worked; my .html files are all parsed as .php.
7
9585
by: Phong Ho | last post by:
I have this problem with PHP that someone might be able to help. With CLI, I am able to execute my .php files. For example, if I type "php phpinfo.php" , this works fine on my system. If I put the phpinfo.php file is in /var/www/html and I try to access it through the browser, I get the content of the file and not the execution of the file. Instead of display the info of PHP, I got the text of the file as:
22
5130
by: nick | last post by:
#include <stdio.h> #define BALANCE 5000 int main(){ int balance = BALANCE; return 0; } when i compile it, an error occurs,what's happen?
21
2649
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225; }; forwarders { 4.2.2.2; }; };
4
10314
by: Mike S | last post by:
I'm hoping someone can explain the following. I have installed Visual Studio 2003. Setup several web application projects, compliled and ran them with no problem. All of this was accomplished with my local server. The default or root directory for my local IIS server is my "C:\" drive. (c:\inetpub\wwwroot) Now I would like to change my root or default directory for my local IIS server to be a network drive (x:\inputpub\wwwroot). ...
2
1903
by: Xagon | last post by:
Hi, I'm a newbie to C#, so forgive me if this is a dumb question. What I want to do is parse a directory of IRC logfiles and dump all the .jpg and .gif links to a text file. If I could get a single filename, I could load it into a richedit control and do the parsing there but Directory.GetFiles doesn't seem to do what I want. Any tips on how to accomplisg this task?
13
4174
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any ideas on how to implement this? Thanks!
24
9852
by: Gary Hasler | last post by:
I want apache to parse all .html files for php code. Our 'real' web server is rented from ICDsoft ( php 5 on Apache on Linux whatever), and their instruction is to add the following to the .htaccess file: AddHandler application/x-httpd-php .html ....which works great. However, our local in-office server for testing and viewing our sites is Apache 2.0.55 with PHP 4.4.4 on Windows XP Pro. I have read everything I can find, and have...
0
10751
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8361
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8807
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8701
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8466
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7299
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1588
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.