59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
From: spamfranke at bigfoot.de (Stefan Franke)
|
|
Date: Thu, 29 Apr 1999 18:01:54 GMT
|
|
Subject: HTML "sanitizer" in Python
|
|
References: <s7284e9b.080@holnam.com>
|
|
Message-ID: <372a9bd7.21336470@news.omnilink.de>
|
|
Content-Length: 1280
|
|
X-UID: 969
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|