Home arrow Python arrow Page 6 - Python 101 (part 5): Snake Oil For The Soul

Desperately Seeking Python - Python

Now that you know how to create and use Python's tuples, listsand dictionaries, it's time to get your hands dirty. In this article, findout how to read from, and write to, files on the filesystem with built-inPython methods, and also take a look at some of the new features availablein Python 2.x.

  1. Python 101 (part 5): Snake Oil For The Soul
  2. Air In A Bottle
  3. Treading The Right Path
  4. Standing In Line
  5. Learning To Write
  6. Desperately Seeking Python
  7. String Theory
  8. Pop() Goes The Weasel
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
July 09, 2001

print this article


There are a few other miscellaneous methods you should know about.

The seek() method moves the internal file pointer to a specific bytelocation, while the tell() method returns the current location of the filepointer.

>>> data = open("snakeoil.txt") >>> data.seek(20) >>> data.tell() 20 >>> data.readline() 'rch, scientists at AB Labs have come up with a radical new product, once that promises to improve the quality of life for humans and animals everywhere.\012' >>> data.tell() 173 >>>
The truncate() method is used to truncate a file to a specific byte size.

>>> data = open("snakeoil.txt", "a") >>> data.truncate(175) >>>
And there are also a couple of interesting file properties you might liketo use in your Python programs. The "mode" and "name" properties return thefile mode and file name respectively, while the "closed" property returns aBoolean value indicating whether or not the file is closed.

>>> data = open("snakeoil.txt", "a") >>> data.mode 'a' >>> data.name 'snakeoil.txt' >>> data.closed 0 >>> data.close() >>> data.closed 1 >>>
Python also comes with an "os" module, which adds a bunch of other filemethods and properties to the built-in ones described above...but I'llleave that to you to experiment with. In case you don't know what a moduleis, don't worry - all will be explained soon!

>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates


Dev Shed Tutorial Topics: