如何覆盖Python中的文件? [英] How to overwrite a file in Python?

查看:138
本文介绍了如何覆盖Python中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖一个文件。我基于我的答案在这个阅读和覆盖Python中的文件



完成我的代码:

  < select class =select compact expandable-list check-list
ONCHANGE =location = this.options [this.selectedIndex] .value;>
< option value ={%url envelopes:auto_sort%}?sort_by = custom>
自定义订单
< / option>
< optgroup label =Category>
< option value ={%url envelopes:auto_sort%}?sort_by = cat_asc>
升序
< / option>
< option value ={%url envelopes:auto_sort%}?sort_by = cat_desc>
降序
< / option>
< / optgroup>
< / select>
$ b $ def auto_sort(request):
sort_by = request.GET.get('sort_by',None)
如果sort_by:
temp_path ={0} / file.txt.format(settings.SITE_ROOT)
$ bf = open(temp_path,'r +')
text = f.read()
text = re.sub(' cat_asc','cat_desc',text)
f.seek(0)
f.write(text)
f.truncate()
f.close();

handle = open(temp_path,'w +')
handle.write(sort_by)
handle.close();

return HttpResponseRedirect(reverse('envelopes:editor'))

输出我当前的代码:

当我尝试再次重写为时,该文件包含 cat_desc 定制。它重写为 customc 。注意最后 c ,它必须是 custom



下面是我想要达到的目的:


  1. cat_desc

  2. 如果我想再次写入,例如 custom ,必须删除 cat_desc ,并用自定义替换


解决方案

根据您的修改的问题,也许这样会更简单

 
sort_by = request.GET.get('sort_by',None)
如果sort_by:
temp_path ={0} / file。 txt.format(settings.SITE_ROOT)
#根据您的逻辑,将new_text设置为您想要的值
new_text ='custom'
f = open(temp_path,'w')
f.write(new_text)
f.close();

handle = open(temp_path,'w +')
handle.write(sort_by)
handle.close();

return HttpResponseRedirect(reverse('envelopes:editor'))


I'm trying to overwrite a file. I based my answer on this Read and overwrite a file in Python

To complete my codes:

<select class="select compact expandable-list check-list" 
    ONCHANGE="location = this.options[this.selectedIndex].value;">
    <option value="{% url envelopes:auto_sort %}?sort_by=custom">
        Custom order
    </option>
    <optgroup label="Category">
        <option value="{% url envelopes:auto_sort %}?sort_by=cat_asc">
            Ascending order
        </option>
        <option value="{% url envelopes:auto_sort %}?sort_by=cat_desc">
            Descending order
        </option>
    </optgroup>
</select>

def auto_sort(request):
    sort_by = request.GET.get('sort_by', None)
    if sort_by:
        temp_path = "{0}/file.txt".format(settings.SITE_ROOT) 

        f=open(temp_path,'r+')
        text = f.read()
        text = re.sub('cat_asc', 'cat_desc', text)
        f.seek(0)
        f.write(text)
        f.truncate()
        f.close();

        handle=open(temp_path,'w+')
        handle.write(sort_by)
        handle.close();

    return HttpResponseRedirect(reverse('envelopes:editor'))

The output of my current codes:

The file contains cat_desc when I try rewrite again as custom. It rewrites as customc. Notice the c at the end, it must be custom only.

Here is what I'm trying to achieve:

  1. I write on file, for example, cat_desc
  2. If I want to write again, for example custom, the cat_desc must be removed and replaced with custom.

解决方案

Based on your revised question, maybe something like this would be simpler

def auto_sort(request):
    sort_by = request.GET.get('sort_by', None)
    if sort_by:
        temp_path = "{0}/file.txt".format(settings.SITE_ROOT) 
        #Set new_text to whatever you want based on your logic
        new_text = 'custom' 
        f=open(temp_path,'w')
        f.write(new_text)
        f.close();

        handle=open(temp_path,'w+')
        handle.write(sort_by)
        handle.close();

    return HttpResponseRedirect(reverse('envelopes:editor'))

这篇关于如何覆盖Python中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆