Exercises, examples and other material relating to training module Y110. This topic is presented on public courses Learning to program in Python
, Python Programming
How to open a file in Python and how to read it line by line into a string, or all at once, into a list. Also how to overwrite a file or add to an existing file, and how to check existing files on your file system.
|Articles and tips on this subject||updated|
|4593||Command line parameter handling in Python via the argparse module|
A new example from the Python course just completed - looking at command line parameters through the argparse module from the standard Python library (2.7 and 3.2 onwards), and XML handling through the xml.etree.ElementTree module (2.5 onwards)
Here's code setting up an argument parser:
|4451||Running an operating system command from your Python program - the new way with the subprocess module|
Python's subprocess module has replaced a series of other modules, and if you're writing new code which calls operating system commands you should use this module in perferance to the older stuff. Easy examples at [here].
The "interesting bit" in various languages is not so much how you cal other ...
|4438||Loving programming in Python - and ready to teach YOU how|
I like programming in Java, but I love programming in Python. It's been a real pleasure to get back to Python this morning. I'm teaching a private course in Cambridge this week, and a public python course the following week. And a new example as work my hand back in ...
Scenario - I require to read ...
|3764||Shell, Awk, Perl of Python?|
Very frequently, IT system users find they've got a data file in one format and they need to filter it and transform it. On Linux and Unix systems, utilities such as grep and cut were the tradtions, perhaps with some awk.
Perl provided a single-program replacement for shell scripts that bolted together ...
|3558||Python or Lua - which should I use / learn?|
"Should I learn and use Python or Lua" - a question to me. The questioner was perceptive, knowing that he shouldn't try to learn lots of languages at the same time, and that he should always limit the "basket" of languages used within his applications - in fact, he was wondering if he could select just ...
|3465||How can I do an FTP transfer in Python?|
"If you think someone's probably done it before, they probably have .... and it's probably available to you" A good answer to many questions of the form "can I do xxxxx in Python?"
Question today "can I do an FTP transfer from Python"
Answer "Yes" [but I had a look around and discovered there was on ...
|3442||A demonstration of how many Python facilities work together|
Many of our demonstrations on the Well House Consultants site show individual features of a language - taken in isolaion to show you how they work. However, during a course we usually write further examples to show you how features work in combination to give a total result / solution to an application.
|3083||Python - fresh examples from recent courses|
During courses, I write a lot of examples in front of delegates so that the get to see both a program to perform a certain task and how that solution was reached - after all, they'll be expected to modify and write programs after the course, and just showing them the final result would only be telling ...
|2870||Old prices - what would the equivalent price have been in 1966?|
What did a cup of tea in a café cost 10 years ago? 20 years ago?
What was the rail fare from London to Manchester in 1966, 1976, 1986, 1996 and 2006?
Such questions prove to be very hard to research and find answers for on the Internet. I suspect that's partly because the Internet wasn't around ...
|2282||Checking robots.txt from Python|
The robots.txt file - which well behaved automata check to see whether they are welcome on a web site - has two directives in its base specification ' User-Agent and DisAllow. You will find some other directives used, and you will find some sites who have a robots.txt file that has blank lines after ...
|2011||Conversion of OSI grid references to Eastings and Northings|
In both Ireland and the UK, the Ordnance survey divides down the country into a series of lettered 100km x 100km squares, represented by 1 letter (Ireland) or 2 (UK mainland), and then measures / reports and Easting / Northing within that square.
ST645332 (UK, box ST, 64.5 km east, 33.2 km ...
|1442||Reading a file multiple times - file pointers|
When you open a file for read, you create a "file pointer" which knows where in the file you are, and each time you read from the file this advances so that you get the next piece of data each time you do a read. Usually you don't see the file pointer at all - it's internal - and you think nothing ...
|183||The elegance of Python|
If I need a little utility and I'm running a Perl course, then I'll write it in Perl. Training in Tcl ... then that's the scripting language to use for other requirements that day (two languages in a day is too much). And of course if I'm running Python Programming it has to be .....
MY GOODNESS - ...
Examples from our training material
|coster.py|| Read whole file, report on lines matching a pattern|
|costhuge.py|| Reading a file line by line|
|dirStream.py|| Object using generator - directory file traverser|
|dmoz.py|| Parsing a large file, looking for matching lines|
|dsa|| polymorphism in action|
|fff|| Jumping around an input file (random access)|
|fgfg|| Improved version - program to handle data recently appended to a log file|
|file_first|| Short file parser|
|filechex|| File checks - is it a directory, when was it modified|
|flr|| Fixed Length Record to CSV|
|grabpage.py|| Read from a URL as is reading from a file|
|grounds.py|| Soccer grounds - Python|
|inflate|| Inflation Calculcator - what would it have cost?|
|is.xyz|| Inflation data - UK - annual factors|
|j2|| using a generator to source data from a file|
|jdemo|| json and csv handling|
|lost.py|| Trapping a non-existant file error|
|majorcooker.py|| Handling huge data|
|n2|| Find the title of a web site|
|pinorder.py|| List a directory sorted by age of object|
|pstat.py|| Checking whether a file exists, and things about it ...|
|sp|| running OS subprocesses in Python|
|sys.py|| Disc space free within an application|
|tablog.py|| File overwrite and append in python|
|wac1|| Read file into list and process each line|
PicturesWriting a Python program
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Opening and reading from files.
File system Checks.
Reading a web page.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.