[Tutor] Reading and Writing

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


["Erik Price"="Erik"]
Erik >> On Thursday, August 29, 2002, at 12:55  PM, Nicole Seitz wrote:
Erik >> 
Erik >> >infile = 
Erik >> >open("C:\WINDOWS\Desktop\python_scripts\html\ez-z.html").read()
Erik >> > outFile = open("C:\WINDOWS\Desktop\python_scripts\info\ez-z.txt","w")
Erik >> >
Erik >> >But there's not only one file to open, there are about 20, all of them 
Erik >> >in the
Erik >> >same directory. My question now is:
Erik >> >
Erik >> >Is it possible to have the program open each of the files in this 
Erik >> >directory
Erik >> >and for each input file, create an output file with a proper name(what 
Erik >> >I mean
Erik >> >is this: for ez-a.htm I like to have ez-a.txt, and so on), so that I 
Erik >> >have to
Erik >> >run my program only one time?
Erik >> 
Erik >> Sure.  Write your script such that it loops through each file in your 
Erik >> target directory and performs the work you need done on each file.  A 
Erik >> simple way to do that is to read all of the files in a directory into a 
Erik >> list, then loop over the list and perform the work to each file in the 
Erik >> list.  The following code should give you an idea of how to do it, but 
Erik >> you might want to add some code that ensures that only certain files 
Erik >> are acted upon (based on filename, for instance -- you can do this with 
Erik >> the "fnmatch" module):
Erik >> 
Erik >> <code>
Erik >> 
Erik >> import os.path
Erik >> 
Erik >> htmlDirectory = ""		# place the path to your read directory 
Erik >> between the quotes
Erik >> txtDirectory = ""			# place the path to your write 
Erik >> directory between the quotes
Erik >> os.chdir(htmlDirectory)
Erik >> 
Erik >> for file in os.listdir():
Erik >>   nosuffix = file.split('.')[0]
Erik >>   infile = open(file).read()
Erik >>   outfile = open(txtDirectory + nosuffix + '.txt', 'w')
Erik >> 
Erik >> </code>
Erik >> 
Erik >> This will open each file in the target directory for reading, then open 
Erik >> a similarly-named file in another directory (the difference is that it 
Erik >> should have the suffix ".txt" instead of ".html") for writing.  Note 
Erik >> that this code doesn't actually do the writing of the first file, I 
Erik >> didn't include that because this is just a different version of the 
Erik >> code you already posted, and that code didn't have the writing part 
Erik >> either.  So you still have to implement the writing of data to the 
Erik >> outfile.
Erik >> 
Erik >> 
Erik >> HTH,
Erik >> 
Erik >> 
Erik >> 
Erik >> Erik
Erik >> 
Erik >> PS: if someone knows of a more efficient solution that doesn't use 
Erik >> os.chdir() I would be very interested in hearing about it -- it seems 
Erik >> that using os.chdir() is overkill for this but maybe not?
Erik >> 

os.listdir() takes a optional path to a directory as an argument.

So you could do something like:

			<code>
#!/usr/bin/env python

import os.path
import fnmatch

text_directory = "/home/ibraheem/python_scripts/txt/"
html_directory = "/home/ibraheem/python_scripts/html/"

for filename in os.listdir(html_directory):
  if fnmatch.fnmatch(filename, "*.html"):
    nosuffix, ext = os.path.splitext(filename)
    in_filename = os.path.join(html_directory, filename)
    out_filename = os.path.join(text_directory, nosuffix + '.txt')
    infile_data = open(in_filename)
    outfile_data = open(out_filename,'w')
    # do something
    close(in_filename)
    close(out_filename)
			<code/>

Not tested.


Kindest regards,

				--ibz.

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