My first python script

Yesterday, I needed a shell script to do a demo of a program, and I thought about python, so my first script was released today. But I have to say, it is useless, because I haven’t reach my goal :(  .  The problem is not in the script but in the program, so the only thing I can do is to abort, although I do not want to say this word. I want to paste some code here because I want to leave some useful command in my own space, so I can re-read them later ^_^

#!/usr/bin/python
# import some useful modules
import os
import glob
import sys
import re
import shutil
import linecache

# def some functions
# get the filename without pathname
def get_filename(f_fullname):
return os.path.split(f_fullname)[1]

# generate filename by replacing the str in filename
def gen_filename(f_filename, f_src_ext, f_dst_ext):
return re.sub(f_src_ext, f_dst_ext, f_filename)

# filter filename by extension and return a list
fullname = glob.glob(‘res/bmp/*.bmp’)

# get the filename(without extension) and return a new list
bmp_file = map(get_filename, fullname)

# generate some filename
ppm_file = [gen_filename(file, 'bmp', 'ppm') for file in bmp_file]
pgm_file = [gen_filename(file, 'bmp', 'pgm') for file in bmp_file]
icp_file = [gen_filename(file, 'bmp', 'icp') for file in bmp_file] ……….

The module shutil allows u using copy and move, and the linecache module supports reading a whold line from a file, they both are useful

Just seen some doc of python, it seems that this is a powerful tools, I hope that I can study it deeper ^_^

This entry was posted in Happy coding. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>