Note that there are some explanatory texts on larger screens.

plurals
  1. POReplacing a section of text in one file with text from another using python
    primarykey
    data
    text
    <p>First a bit of background: yes, I am new to python, but I like to dabble and learn things. </p> <p>The goal is this: I have an intranet website here at work and they have us on a static server with no server side scripting allowed, meaning no PHP. So as I add new pages, I must update every freakin page's menu with the new link. Fortunately, I have an application called ArcGIS installed on my computer and with it came an installation of python. So I was thinking it would be nice to just put together a script that would read a file called "menu.txt" and then search recursively in my directory (and subdirectories) for files with ".html" and replace all text between some comment tags, like <code>&lt;!--begin menu--&gt;</code> and <code>&lt;!--end menu--&gt;</code>, with the text in "menu.txt"</p> <p>So I started looking and found this snippet of code:</p> <pre><code>with open('menu.txt', 'r') as f: # read entire file into file1 # ASSUMING 'file1.txt' is relatively small... file1 = f.read() with open('test.html', 'r') as f: # ASSUMING 'file2.txt' is relatively small... file2 = f.read() # read file into file2 # index() will raise an error if not found... f1_start = file1.index('&lt;!--123--&gt;') f1_end = file1.index('&lt;!--321--&gt;', f1_start) # look for '//end' after '//start' f2_start = file2.index('&lt;!--123--&gt;') f2_end = file2.index('&lt;!--321--&gt;', f2_start) # replace file2 lines with proper file1 lines file2[f2_start:f2_end] = file1[f1_start:f1_end] with open('test.html', 'w') as f: f.write(file2) </code></pre> <p>and I've also seen many examples using <code>re</code>, <code>replace</code> and such, but nothing that seems to be related to what i need. Anyway, right now I'm just trying it on the one file in the same directory, but when I run this on either my linux machine or in a windows python shell I get:</p> <pre><code>Traceback (most recent call last): File "P:\webpages\filereplace.py", line 18, in &lt;module&gt; file2[f2_start:f2_end] = file1[f1_start:f1_end] TypeError: 'str' object does not support item assignment </code></pre> <p>I thought the problem might've been the <code>with open</code> part, but I don't know.</p> <p>In this case the contents of menu.txt are essentially a beginning comment tag <code>&lt;!--123--&gt;</code>, then all of the <code>&lt;div id=menu&gt;blah blah blah&lt;/div&gt;</code>, then an end comment tag <code>&lt;!--321--&gt;</code>. In my html file, I use the same comment tags, and you get the picture...</p> <p>Any suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload