iFocus.Life News News - Breaking News & Top Stories - Latest World, US & Local News,Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The iFocus.Life,

What Is a File Descriptor, Anyway?

106 51
It may seem obvious sometimes, but one of the keys to software re-use is abstraction. For this reason, Python does not handle files in terms of their names but in terms of number value. Rather than process the file as "This is file named 'simple.txt'", it works with it as "This is file three, the one after file two and before file four." Consequently, after you tell Python to open a file, it deals with it by its number, not by its name.

To illustrate this, we'll save a file to your computer's hard disk and use it to illustrate Python's file descriptor handling. So press 'Control-s', or whatever it takes to save a file from your web browser, and save this file as 'python-fd.html'. Note the directory into which you are saving it; you will need that information in a moment.

Now, start a Python shell session. Import the os module:

import os And open the saved file with a file handle of your choice. Here we will use the innovative handle input:
input = open('/absolute/path/to/the/saved/file/python-fd.html', 'r') Note that we are using the absolute path and are explicitly telling Python to open the file as 'read only'. You need do neither, but both are good practice. If you opened the Python shell from the directory in which the file is located, you can simply use dot-slash shorthand: './python-fd.html'. The initial dot tells Python to look in the present directory for the ensuing file path or name. The default for the open() function is read-only (type 'help(open)' in a Python shell to read more about this).
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like on "Technology"

Leave A Reply

Your email address will not be published.