My very first Python script : DocFinder

As you may already have noticed, I am quite found of Python as a programming language.

I fell in love with Python some years ago, but you should know more about this soon icon smile My very first Python script : DocFinder .

As many of Python users, I started with Dive into Python (moreover its french translation).

Some days ago, I found myself into the very first Python script I ever written (but still use some times icon smile My very first Python script : DocFinder ). I found it funny (even more when you think that It can be reduced to a one-liner), and thought I would share it.

It aims at listing all documents from a given extension in a folder. To use it, simply run


$ docfinder folder extension

So here is the code :

#!/usr/bin/python
#_*_ coding: ISO-8859-15 _*_
import os
import sys

def scan_rep(repertoir, extension):
    """scanne le rep courant pour trouver des tex"""
    for racine, reps, fichiers in os.walk(repertoir, topdown=True):
        for fichier in fichiers:
            if fichier.endswith('.%s' % extension):
                nom_complet=os.path.join(racine, fichier)
                print '%s '%\(nom_complet)
if __name__=='__main__':
    scan_rep(sys.argv[1],sys.argv[2])

Pretty lame, isn’t it ? icon smile My very first Python script : DocFinder

When you find yourself a bad programmer, it is always good to look at the past and see that you actually made some progress icon smile My very first Python script : DocFinder .

You might want to check my current pet project, it is also all Python icon smile My very first Python script : DocFinder

Julien

flattr this!

Count the number of lines of code of your projects

After having finished a project, I always like to know its actual size. It does not give any value to my work, but it is always fun to see icon smile Count the number of lines of code of your projects

I found this simple command that will pop out the number of lines of code for a given file extension.


cat `find . -name "*.py"` | wc -l

You can also do it for a whole folder


find . -name "*.py" | xargs wc -l

flattr this!

Easily change file separator

You may already have found yourself in front of a huge csv file to be processed. Five minutes after having started working, you realize that you want spaces instead of commas in your file. Only problem, the file is 500 megabytes big.

Do you run Linux ? In this case, your case will be solved in 10 seconds (who said as usual ? :p).

Simply open a console and run this line


sed -e $"s/,/\ /g" myfile > newfile

or more generally,


sed -e $"s/old_separator/new_separator/g" myfile > newfile

If not, well you may want to think about switching then icon smile Easily change file separator

Thanks to Frafra for the tip icon wink Easily change file separator

flattr this!