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 (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. :) |
I believe os.walk is the method you're looking for for drilling down a directory.
|
Another non-python solution would have been:
Code:
save as helper.sh Code:
find \( -iname "*.h" -o -iname "*.cpp" -o -iname "*.hpp" \) -exec helper.sh {} \; Cheers, Tink |
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 |
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 |
A bash script that goes through all subdirectories:
Code:
#!/bin/bash |
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. |
All times are GMT -5. The time now is 02:09 AM. |