Python 2.7 bottle web
Question: I’m trying to figure out how to rename an existing text file when I change the
title of the text file. If I change the title now, it’s going to create a new
text file with the new title. The "old text file" that I wanted to save with a
new name still exists but with the orginal name. So i got two files with the
same content.

I’m creating new articles (text files) through @route('/update/',
method='POST') in my ”edit templet” where title=title, text=text. Let’s say
after I have created a new article with the name(title) = ”Key” and wrote a
bit in that text file. Then If I want to edit/change my ”Key” article I click
on that article and present the article in @route('/wiki/',)def
show_article(article):. title = article, text = text)

In this template I can change my ”Key” name(title) to ”Lock”. I’m still using
the same form @route('/update/', method='POST') to post my changes. Here is
the problem, it creates a new text file instead of renaming the ”Key” article
to ”Lock”.

How can I change the @route('/update/', method='POST') to make it realise that
I’m working with an already existing text file and only wants to rename that
file. I have tried to use two different method=’POST’ but only gets method not
allowed error all the time.

    
    
            from bottle import route, run, template, request, static_file
            from os import listdir
            import sys
            host='localhost'
    
            @route('/static/<filname>')
    
            def serce_static(filname):
                return static_file(filname, root="static")
    
            @route("/")
            def list_articles():
                files = listdir("wiki")
                articles = []
    
                for i in files:
                    lista = i.split('.')
                    word = lista[0]
                    lista1 = word.split('/')
                    articles.append(lista1[0])
    
                return template("index", articles=articles)
    
    
           @route('/wiki/<article>',)
            def show_article(article):
                wikifile = open('wiki/' + article + '.txt', 'r')
                text = wikifile.read()
                wikifile.close()
    
                return template('page', title = article, text = text)
    
    
            @route('/edit/')
            def edit_form(): 
    
                return template('edit')
    
            @route('/update/', method='POST')
            def update_article():
    
                title = request.forms.title
                text = request.forms.text
                tx = open('wiki/' + title + '.txt', 'w')
                tx.write(text)
                tx.close()
    
                return template('thanks', title=title, text=text)
    
            run(host='localhost', port=8080, debug=True, reloader=True)
    


Answer: You can use `os.replace('old_name', 'new_name')`:

    
    
    import os
    ...
    tx = open('wiki/' + title + '.txt', 'w')
    tx.write(text)
    os.replace(tx.name, 'name_you_want.txt') # use os.replace()
    tx.close()
    

