The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Stripping out extension in file name devs Shell Programming and Scripting 9 05-14-2008 04:53 AM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 08:40 AM
trim file name extension???? vishal_ranjan AIX 1 07-31-2007 09:34 AM
distinguish the extension of a file tbeghain AIX 2 06-13-2007 11:15 AM
Check file extension mahalakshmi Shell Programming and Scripting 6 12-27-2006 01:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-01-2007
Phobos33 Phobos33 is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
Run a program for every file with a given extension

Hey all

I have written a python program that takes as input the name of a data file with the extension .lc I would like to write a shell script that finds all of the .lc files in a set of branching directories and runs the program.

Any help?
  #2 (permalink)  
Old 06-01-2007
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
Use the 'find' command for that. Run the output of that command through a for or a while loop that will run your python command for each file that is found.
  #3 (permalink)  
Old 06-01-2007
Phobos33 Phobos33 is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
err.... anything more specific than that... im new to all this.
so i use find to create an array that stores all of the files i will need and then i run my program for each on inside a for loop??
  #4 (permalink)  
Old 06-01-2007
Phobos33 Phobos33 is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 3
Hey
When you say pipe it into python, how do i pipe it in as one of the arguments

find /path -type f -name "*.lc" -print | python tester.py ########

I want to put the output where ######## is.

Thanks
  #5 (permalink)  
Old 06-01-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,522
here's a snippet. Use the sys.stdin.readlines() method in your python script
Code:
import sys
print sys.stdin.readlines()
then from shell:
Code:
# find /path-type f -name "*.lc" -print | ./test1.py
['/path/user/test.lc\n', '/path/test/test1.lc\n']

Last edited by ghostdog74; 09-20-2008 at 09:11 AM..
  #6 (permalink)  
Old 06-01-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,522
you can find those files in Python itself. this way, you don't have to maintain an extra shell script.
some of the modules in Python you can use to find files are: os.walk(), glob , os.path.splitext() etc...

here's a snippet:
Code:
#!/usr/bin/python
import os
for root,dir,files in os.walk("/home"):
    for file in files:
        if file[-3:]==".lc":
            fullpath=os.path.join(root,file)
            do_something(fullpath)
however, if you want to use the shell to find those files, you can use "find" command
Code:
find /path -type f -name "*.lc" -print > file # or pipe to you python program
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0