Showing posts with label OS Module. Show all posts
Showing posts with label OS Module. Show all posts

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.

Thursday, April 12, 2012

File Handling In Python II

Hello, everyone in this post, I will discuss the some more features of File Handling in Python, the very basics that you would need for handling and playing around with files in host operating system.

From the last post, we know how to create, append and read a text file in the root directory that is the C Drive of a Windows Operating system. Now what if we want to make a directory or probably create a text file somewhere else, but not in the C Drive.

So in this post, I will focus on how to jump around folders and directories.

For this we need to import a Module, named OS which is built into Python.

 >>> import os
>>> os.getcwd() <-- Returns the current working folder
'C:\\Python27'
>>> os.curdir <-- Returns the current working Drive or directory
'.'
>>> os.name <-- Returns the platform of a host Operating system.
'nt'
>>> os.mkdir('C:/PyProg') <-- Creates a New Folder
>>> os.mkdir('C:/PyProg/My_Project') <-- Creates a New Folder
>>> os.getcwd()
'C:\\Python27'
>>> os.chdir('C:/PyProg/My_Project') <--- Changes Directory
>>> os.getcwd() <-- Just a Proof, That The current Working Directory is different
'C:\\PyProg\\My_Project'
>>> os.environ <--- Returns the Environment Variables Of the Host OS.
[Try it out yourself, I am not showing the Output here, For some reason]
>>> os.listdir('C:/') <-- Directory Listing Of a Drive Or a Folder
['.rnd', 'ArrayDemo1.java', 'AUTOEXEC.BAT', 'boot.ini', 'Config.Msi', 'CONFIG.SYS', 'copy', 'dir1', 'Documents and Settings', 'files', 'Hacked.html', 'hacked.txt', 'Havij1.14Free.exe', 'Inetpub', 'Intel', 'IO.SYS', 'Java', 'log.txt', 'MSDOS.SYS', 'MSOCache', 'My Web Sites', 'NETCAT', 'NTDETECT.COM', 'ntldr', 'pagefile.sys', 'php', 'Program Files', 'pyinstaller', 'PyProg', 'Python27', 'pytoexe', 'RECYCLER', 'RHDSetup.log', 'ruby', 'streams.exe', 'System Volume Information', 'TC', 'Tc.exe', 'Tc.r00', 'Tc.r01', 'tests', 'themole-0.2.6', 'Visual Python', 'wamp', 'webgrabber', 'wget.exe', 'WINDOWS']
>>> os.getcwd()
'C:\\PyProg\\My_Project'
>>> myFile = open('Pythontext.txt', 'w')
>>> myFile.write(''' Hey, This are just the basics to show you how to use the OS Module with few examples.
If You want to Dive Deep into the Module.
Type, dir(os) in the Python Shell, To check the functions of the OS Module.
You can also type, help(os) to get a detailed report of the OS Module.
If you want to be specific about any function in the OS Module.
You need to type. help(os.function_name_of_any_OS_Module)''')
>>> myFile.close()
>>> os.getcwd()
'C:\\PyProg\\My_Project'
>>> os.listdir('C:/PyProg/My_Project')
['Pythontext.txt']
>>> myFile = open('Pythontext.txt')
>>> for lines in myFile:
                print lines

   
Hey, This are just the basics to show you how to use the OS Module with few examples.

If You want to Dive Deep into the Module.

Type, dir(os) in the Python Shell, To check the functions of the OS Module.

You can also type, help(os) to get a detailed report of the OS Module.

If you want to be specific about any function in the OS Module.

You need to type. help(os.function_name_of_any_OS_Module)
>>>
>>> myFile.close()

Well, These are the basic things that you would need very often, practice them, and Don't Forget to Check My next Post.

Thank You!