LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-22-2004, 02:12 PM   #1
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 31
how do I do this in Python? (simple sh script)


I recently had to convert a directory full of source files from dos-style to normal unix-style line endings. I ended up dashing off a simple bash script, something like:
Code:
#!/bin/bash

for file in *.{cpp,h,hpp}
do
    dosunix $file XXX.newfile
    rm $file
    mv XXX.newfile $file
done
This wasn't ideal, since I had to manually cd into various subdirectories and run it again from there.

(Also note that dosunix is the dos2unix available via fink on Mac OS X).

I'm learning Python ATM, and am curious: how could I have done this in Python? (Once I get this simple version working, I think the really fun part will be getting it to recursively traverse subdirectories and hit 'em all. :)

Last edited by johnMG; 12-22-2004 at 02:13 PM.
 
Old 12-22-2004, 02:43 PM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
I believe os.walk is the method you're looking for for drilling down a directory.
 
Old 12-22-2004, 03:21 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

Rep: Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880Reputation: 880
Another non-python solution would have been:

Code:
save as helper.sh
dosunix $1 XXX.newfile
rm $1
mv XXX.newfile $1
chmod that and put somewhere in path ...

Code:
find \( -iname "*.h" -o -iname "*.cpp" -o -iname "*.hpp" \) -exec helper.sh {} \;

Cheers,
Tink
 
Old 12-22-2004, 04:10 PM   #4
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Original Poster
Rep: Reputation: 31
sigsegv, thanks for the great hint.

Now, how do I run dosunix on each item in a list of filenames (once I get that list via os.walk)?

Code:
#!/usr/bin/python
#...
for file in list_of_files:
    floob( 'dosunix', file, 'XXX.newfile' )
    floob( 'rm', file )
    floob( 'mv', 'XXX.newfile', file )
Tink -- thanks for the alternative.
 
Old 12-22-2004, 06:14 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
I'm a newbie in python, so this may not be very optimal. But it works for me.
Also I think it would be nicer to do the dos-unix conversion in python as well.
Code:
#!/usr/bin/env python

import os

for root, dirs, files in os.walk('/your/directory'):
    for name in files:
        if os.path.splitext(name)[1] in ['.h', '.cpp', '.hpp']:
            file = os.path.join(root, name)
            temp = file + '.tmp'
            os.system('dosunix ' + file + ' ' + temp)
            os.rename(temp, file)

Last edited by Hko; 12-22-2004 at 06:33 PM.
 
Old 12-22-2004, 06:25 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
A bash script that goes through all subdirectories:
Code:
#!/bin/bash

find -type f -regex '.*\.cpp\|.*\.h\|.*\.hpp' | while read F ; do
	dosunix $F ${F}.tmp
	mv ${F}.tmp $F
done

Last edited by Hko; 12-22-2004 at 06:29 PM.
 
Old 12-22-2004, 10:05 PM   #7
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Original Poster
Rep: Reputation: 31
Ahh... os.system().

I just found commands.getoutput( 'some_command' ).

Hmm... looks like you use commands.getoutput() when you want to save the return value into a variable, and you use os.system() when you want the output going to the console.

Sweet! Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
newbie, getting simple python script to work Monjawi Programming 6 10-03-2005 11:51 AM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 11:43 AM
simple python function question Tyir Programming 2 03-28-2004 10:53 PM
Python how name variable inside a class - very simple? lugoteehalt Programming 5 10-22-2003 05:11 AM


All times are GMT -5. The time now is 05:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration