HTML "sanitizer" in Python

Stefan Franke spamfranke at bigfoot.de
Thu Apr 29 14:01:54 EDT 1999


On Thu, 29 Apr 1999 12:20:27 -0400, "Scott Stirling" <SSTirlin at holnam.com>
wrote:

>On opening files in Windows--I was hoping there was a way to give python the full file path.  
>Everything I have seen so far just tells me how to open a file if it's in the same directory I am 
>running python from.
Uuh,

   f = open ("c:/my_path/my_file.txt", "r")

Every in function in the Python library that has a file name argument
accepts a full/relative path also (except when dealing with path and
name components explicitely)!

Note the normal slashes.. with backslashes you had to write

   f = open ("c:\\my_path\\my_file.txt", "r")

or

   f = open ( r"c:\my_path\my_file.txt", "r")

because Python uses the backslash as an escape character inside
string literals, which can be suppressed by using "raw" strings with
a leading 'r'.

Here's a quick outline of some file processing of your kind, which
may give you a first impression (typed without testing):

source = open("/path/file.txt", "r")
dest = open("/path/file.txt", "w")

content = source.read()         # read the entire file as a string

# Do some processing, perhaps
import string
string.replace (content, "some_substring", "by_another")

dest.write (content)
source.close()
dest.close()


Hope that helps,
Stefan





More information about the Python-list mailing list