a python script to change file extensions of all the files in a directory:
_____________________________________________________________________________________________
#!/usr/bin/env python
import os
import string
my_directory=’/home/ross’
for object in os.listdir(my_directory):
if os.path.isfile(os.path.join(my_directory, object)):
req_file=object
if ‘.py’ in req_file:
ren_file=req_file.replace(‘.mp3′, ‘.mp4′)
old_file=os.path.join(my_directory, req_file)
new_file=os.path.join(my_directory, ren_file)
os.rename(old_file, new_file)
_____________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
a simple script to count the no. of lines in a file:-
____________________________________________________________________________________________
#!/usr/bin/env python
read_file=open(‘/testfile.txt’, ‘rb’)
count_file=len(read_file.readlines())
print count_file
____________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)
A simple python script to copy a content of one file to another.
________________________________________________________________________________________________________
#!/usr/bin/env python
to_readfile=open(‘/testfile.txt’, ‘r’)
try:
reading_file=to_readfile.read()
writefile=open(‘/testfile2.txt’, ‘w’)
try:
writefile.write(reading_file)
finally:
writefile.close()
finally:
to_readfile.close()
________________________________________________________________________________________________________
(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Now I am not gonna write any history about the Python programming language, If you would like to know the history and other related stuff you can check out python on wikipedia. Working as as system admin, most of the time many of us are hooked on to the bash and shell scripting, as that is the simplest programming stuff that admins can learn quick and easily and implement it. Because most the scripts written by admins are kind of Monitoring, Backup, or updation scripts.
Last month i got hooked on to the Python programming language, for the past few months i visited some tech blogs and got some good articles on the Python thing and started visiting python.org quiet often and reading about the number of organizations and success stories, thought of giving a try, I planned to learn it all by myself, first thing started googling online pdf stuff to learn python as a beginner. After going through many online tutorials and pdfs i Slected the Beginning Python: From Novice to Professional By Magnus Lie Hetland, great stuff and it helped me in learning and understanding python, i loved this Book, it cleared my basics on python (well, i didn’t do the projects that were mentioned, but i would suggest, if you select this same book, please also try for the projects, it would give you a good hands on). after that my 2nd Book was Python:Create-modify-Reuse By Jim Knowlton, This was a good book to get further practical in Python and gave me quiet little confidence that yes i can write python code. My 3rd and 4th Books which i still keep at hand for my reference are the Python for Unix and Linux System Administration By Noah Gift & Jeremy Jones, And Python CookBook By Alex Martelli & David Ascher. As for the Python CookBook, the Book is quiet Heavy to be carried daily to Office, so you can refer it online at the Following URL:- http://aspn.activestate.com/ASPN/Cookbook/Python.
Read more…