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!

0 comments:

Post a Comment