File Handling in Python; Resources...

Introduction...

Welcome! The following is a list of practical Python file handling methods, which should prove useful when put into practice. Feel free to use this Tackk as an exam revision source, or a Computing Controlled Assessment reference for programming techniques. Enjoy!

Useful Websites...

Before we begin, I think it's necessary to include a bibliography of my sources, as well as a documentation of where to find this sort of stuff. So, to begin with, let's list some very useful websites, which should all yield some fantastic results if you're looking for techniques and other methods. Here are some websites which should be beneficial...

http://www.tutorialspoint.com/python/python_files_...

https://www.python.org/doc/

https://docs.python.org/2/library/stdtypes.html

https://docs.python.org/2/library/string.html

https://docs.python.org/release/2.5.2/lib/string-m...

File Handling...

As mentioned in the title, the following is a list of techniques and operators one can use when handling external data in Python. Reference: EOF/EoF is an abbreviation for "End of File".

  • close(): This closes the file. A closed file cannot be read or written any more. Any operation which requires that the file be open will raise a “ValueError” after the file has been closed. Calling “close()” more than once is allowed.
  • next(): A file object is its own iterator, for example, “iter(f) returns f (unless f is closed)”. When a file is used as an iterator, typically in a for loop, (for example, for line in f: print line), the “next()” method is called repeatedly. This method returns the next input line, or raises "StopIteration" when the EOF is hit. In order to make a "for" loop the most efficient way of looping over the lines of a file, (a very common operation), the “next()” method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining “next()” with other file methods, (like "readline()") does not work as it should. However, using “seek()” to reposition the file to an absolute position will flush the read-ahead buffer.
  • read([size]): Read at most size bytes from the file, (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
  • readline([size]): Read one entire line from the file. A trailing newline character is kept in the string, (but may be absent when a file ends with an incomplete line).
  • readlines([sizehint]): Read until EOF using “readline()” and return a list containing the lines thus read.
  • seek(offset[whence]): Set the file's current position, like “stdio's fseek()”.
  • write(str): Write a string to the file. There is no return value. Due to buffering, the string may not actually show up in the file until the “flush()” or “close()” method is called.
  • writelines(sequence): Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. (The name is intended to match “readlines(); writelines()” does not add line separators.).

These are especially useful for handling, editing, reading and overwriting external data and files not stored within Python. For Controlled Assessment purposes, these methods of manipulating external data should really bump up your marks, if you use them correctly, (and well).

This Has Been a GrumblingGrenade Tackk, (@HarbingerOfNonsense), Thank You Very Much For Your Time.

Comment Stream

2 years ago
1

🇬🇩 🇬🇩 🇬🇩 🇬🇩 🇬🇩 🇬🇩 🇦🇮 🇱🇮 🇰🇮 🇸🇲 🇧🇲 🇰🇮 🇸🇲 🇸🇹 🇻🇨 🇩🇯 🇱🇮

2 years ago
0

🇬🇧

2 years ago
0

Just to keep the trend going...