Sunday, April 15, 2012

File Handling In Python IV

Hello everyone, this post would not be a long one, as all the basics has been discussed about file handling.

This is just a quick review of some more File Handling Module that I could not relate to the earlier posts.

Lets get started.

>>> import os
>>> import sys
>>> sys.platform <--More Defined Way To Know About a Host OS
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_mercurial', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_traceback', 'exc_type', 'exc_value', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
>>> sys.version
'2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
>>> sys.winver
>>> sys.subversion
('CPython', '', '') <--Python is Officially known as CPython, other version is Jython.
>>> sys.api_version
>>> sys.getwindowsversion()
sys.getwindowsversion(major=5, minor=1, build=2600, platform=2, service_pack='Service Pack 2')
>>> sys.getsizeof('C:/files')

Play around with the sys module, type help(sys.any_method_of_sys) To get a detailed report of the Functions.

Well, Some Of You now may be thinking, how to handle some other type of file like an Image File, or say pdf, well, for these, Pythons Inbuilt Library is not enough, You have to go out on the web, to search for the Modules.

Make a Note Here, Whenever You Use Modules from the web, make sure to check that the Module is coded for Python 2 series, because Python 3 has got different syntax.

1. For Images: You need to look for PIL(Python Imaging Library) Take help of Google, as I dont want to spoon feed for everything.

2. MS Office: You need a Module named pywin32, or Python Office(especially meant for excel worksheets)

3. PDF: You need a Module named gfx, found under swftools

And a lot more are there, remember, Python is kinda open source, where codes are developed by any one and everyone. But before using any Module, make sure You use a Module from a trusted site, one good source is stackoverflow.

Now, do you remember I said when we create our own Modules we save it in places where Python looks for, one place i mentioned was Pythons root folder. There is one place and recommended as a standard Pythonist, you should always save the modules in site-packages. Example in my case its:

C:\Python27\Lib\site-packages, to verify, on the Python Shell, click on File>>Path Browser>>Expand the [+] C:\Python27\Lib\site-packages, and you should be able to see any new module that you import there.

Okay, so this was pretty much that you need to fire up your Python Instinct, hope this was helpful, Keep Practicing.

Don't forget to check my next post.

Thank You!


Post a Comment