[Tutor] Reading and Writing

Ibraheem Umaru-Mohammed iumarumo@eidosnet.co.uk
Thu, 29 Aug 2002 16:48:10 +0100


["Ibraheem Umaru-Mohammed"="Ibraheem"]
Ibraheem >> 
Ibraheem >> os.listdir() takes a optional path to a directory as an argument.
Ibraheem >> 
Ibraheem >> So you could do something like:
Ibraheem >> 
Ibraheem >> 			<code>
Ibraheem >> #!/usr/bin/env python
Ibraheem >> 
Ibraheem >> import os.path
Ibraheem >> import fnmatch
Ibraheem >> 
Ibraheem >> text_directory = "/home/ibraheem/python_scripts/txt/"
Ibraheem >> html_directory = "/home/ibraheem/python_scripts/html/"
Ibraheem >> 
Ibraheem >> for filename in os.listdir(html_directory):
Ibraheem >>   if fnmatch.fnmatch(filename, "*.html"):
Ibraheem >>     nosuffix, ext = os.path.splitext(filename)
Ibraheem >>     in_filename = os.path.join(html_directory, filename)
Ibraheem >>     out_filename = os.path.join(text_directory, nosuffix + '.txt')
Ibraheem >>     infile_data = open(in_filename)
Ibraheem >>     outfile_data = open(out_filename,'w')
Ibraheem >>     # do something
Ibraheem >>     close(in_filename)
Ibraheem >>     close(out_filename)
Ibraheem >> 			<code/>
Ibraheem >> 
Ibraheem >> Not tested.
Ibraheem >> 
Ibraheem >> 
Ibraheem >> Kindest regards,
Ibraheem >> 

Actually, os.listdir must be specified a path as its argument.
The code above I think needs to import "os" module as well.
Iti also probably neater to use the glob module, as glob returns full
pathnames, whereas with the os.listdir function, we find ourselves
constructing the path. 

Kindest regards,

				--ibz.
-- 
				Ibraheem Umaru-Mohammed 
					"ibz"
			umarumohammed (at) btinternet (dot) com