Saturday, April 14, 2012

File Handling In Python III

Hello everyone, in this post we will discuss more about File Handling with Python..

Okay, so far I have discussed, how to create, append, read and close a File, also discussed about how to check the current directory and change directory, how to make new folder, etc.

In this post, lets start by showing you how to delete a folder and continue with few more functions.

Lets fire up the Python Shell..

>>> os.getcwd()
'C:\\Python27'
>>> os.chdir('C:\\files')
>>> os.listdir(os.curdir)
['i m siiting here.jpg', 'Looping with disctionary.txt', 'morefiles', 'Printout.docx', 'py1.html', 'Python post march20.txt', 'Python Post March21.txt', 'Python Post March22.txt', 'Python Post March25.txt', 'python post19th.txt', 'python.html', 'Python27march.txt', 'sub', 'test.html', 'test1.html', 'tkinter1.pdf', 'whiskey.html']
>>> for files in os.listdir(os.curdir):
             print files

   
i m siiting here.jpg
Looping with disctionary.txt
morefiles
Printout.docx
py1.html
Python post march20.txt
Python Post March21.txt
Python Post March22.txt
Python Post March25.txt
python post19th.txt
python.html
Python27march.txt
sub
test.html
test1.html
tkinter1.pdf
whiskey.html
>>> # Now lets say I want to delete the test.html file
>>> os.remove('test.html') <--- Deletes a File
>>> Note: the parameter it takes is path, but since I am in the same directory, I just entered the file name, But If You want to Delete some thing on a different folder you need to type in the exact path, like C:\\folder\\subfolder\\file
>>> for files in os.listdir(os.curdir):
             print files

   
i m siiting here.jpg
Looping with disctionary.txt
morefiles
Printout.docx
py1.html
Python post march20.txt
Python Post March21.txt
Python Post March22.txt
Python Post March25.txt
python post19th.txt
python.html
Python27march.txt
sub
test1.html
tkinter1.pdf
whiskey.html
>>> #test.html file is missing, means it has been deleted, now lets see how to rename a file, we will rename, we will rename test1.html to sample.html
>>> os.rename('test1.html', 'sample.html')
>>> for files in os.listdir(os.curdir):
             print files

   
i m siiting here.jpg
Looping with disctionary.txt
morefiles
Printout.docx
py1.html
Python post march20.txt
Python Post March21.txt
Python Post March22.txt
Python Post March25.txt
python post19th.txt
python.html
Python27march.txt
sample.html
sub
tkinter1.pdf
whiskey.html
>>>

We have another Function, that helps to walk through a directory, suppose u have a folder, where you have some files, and also few sub folders, where you have some more files. So os.walk() helps to recursively walk through the files. Lets check it out.

>>> path = 'C:\\files'
>>> for root, dirs, files in os.walk(path):
             print root
             print dirs
             print files
             print " "
           
C:\files
['morefiles', 'sub']
['i m siiting here.jpg', 'Looping with disctionary.txt', 'Printout.docx', 'py1.html', 'Python post march20.txt', 'Python Post March21.txt', 'Python Post March22.txt', 'Python Post March25.txt', 'python post19th.txt', 'python.html', 'Python27march.txt', 'sample.html', 'tkinter1.pdf', 'whiskey.html']

C:\files\morefiles
[]
['log.txt', 'myfile.txt', 'RHDSetup.log']
 
C:\files\sub
[]
['animated Pictures.txt', 'ASCII conversion Java.txt', 'average calculator 2 java.txt', 'back track wifi wpa hack.txt', 'backtrack.txt', 'backtrack2resolve.txt', 'bangladesh hackers friend.txt', 'bangladesh site hacked.txt', 'batch virus.txt', 'botnet.txt', 'CBT Sites.txt', 'CEH Linux commands.txt', 'CEHv7.txt', 'check how many times a character appears in word_java.txt', 'computer korner post.txt', 'constructor explained by Onty.txt', 'convertRHELtoCentos.txt', 'cpp keyloger.txt', 'crackinf for this software.txt', 'Cracking tut.txt', 'davemate2bhacked.txt', 'Deface and Shell.txt', 'defacepageCreator.txt', 'DummiesJava.txt', 'facebook emo added.txt', 'FootprintingCEH.txt', 'forum.txt', 'good site.txt', 'hack note.txt', 'hackers shell tool.txt']

 >>> #So what I asked Python is, please look for all the files in the folder named files under C: and display the path, display sub folders and then display the files.

 Yet Another Module, that I would like to present here, is the glob Module, that helps to display files matching a Pattern or regular expression. Lets check it out.

 >>> import glob
 >>> for files in glob.glob('C:\\files\\*.txt'):
             print files

   
C:\files\Looping with disctionary.txt
C:\files\Python post march20.txt
C:\files\Python Post March21.txt
C:\files\Python Post March22.txt
C:\files\Python Post March25.txt
C:\files\python post19th.txt
C:\files\Python27march.txt
>>> for files in glob.glob('C:\\files\\*.pdf'):
             print files

   
C:\files\tkinter1.pdf
>>> for files in glob.glob('C:\\files\\*.jpg'):
             print files

   
C:\files\i m siiting here.jpg
>>>

So you see that you can almost do anything and everything with Python, If you know Him well. Well I guess this was informative and Interesting and one of the important chapters in Python, So practice it well, and Don't Forget to check my next post.

Thank You.

0 comments:

Post a Comment