删除非空目录/return response/update.ini

来源:互联网 发布:淘宝分销平台网址 编辑:程序博客网 时间:2024/06/09 20:18

python中删除非空目录 http://standalone.javaeye.com/category/72705

import shutil

 

shutil.rmtree(dir)

 

python中list类型相减

两个list ()类型相减
就是比如
a=[1,2,3,4,5]
b=[2,3,4]
a-b需要得到[1,5]
但是python list 不支持减法
只能使用py的投机取巧了~ 呵呵
我们来用set() 如下
<code l=python >
print list (set(a)-set(b))
[1,5] #结果

 

///

= open(filename)
data 
= f.read()
f.close()

response 
= HttpResponse(data,mimetype='application/octet-stream'
response[
'Content-Disposition'= 'attachment; filename=%s' % filename
return response

 

///

相对于php做页面,django我很不熟悉。所以询问了熟悉的朋友同事,记录如下。

当然php做下载文件很简单:

header("Content-type: test/html");
header("Content-Disposition: attachment; filename=test.txt");
echo "aa/n";
echo "bb/n";
echo "cc/n";
exit;
 

在django里,定义了url之后,只要在view里添加如下代码匹配就可以了

def download_file(request):
    from django.http import HttpResponse    
    ## CSV
    #import csv   
    #response = HttpResponse(mimetype='text/csv')
    #response['Content-Disposition'] = 'attachment; filename=my.csv'
    #writer = csv.writer(response)
    #writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
    #writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])

    # Text file
    #response = HttpResponse(mimetype='text/plain')                                
    #response['Content-Disposition'] = 'attachment; filename=my.txt'             
    #response.write("aa/n")
    #response.write("bb")
   
    # PDF file
    #http://code.djangoproject.com/svn/django/branches/0.95-bugfixes/docs/outputting_pdf.txt
    from reportlab.pdfgen import canvas  
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
    p = canvas.Canvas(response)
    p.drawString(100, 100, "Hello world.")
    p.showPage()
    p.save()

         
    return response

 

-------------------------------------------------------

 

比如有一个文件update.ini,里面有这些内容:

[ZIP]
EngineVersion=0
DATVersion=5127
FileName=dat-5127.zip
FilePath=/pub/antivirus/datfiles/4.x/
FileSize=13481555
Checksum=6037,021E
MD5=aaeb519d3f276b810d46642d782d8921
那就可以通过下面这些代码得到MD5的值,简单吧
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open('update.ini'))

a = config.get("ZIP","MD5")
print a 
××××××××××××××××××××××××××××××××××××××××××××××××
写也很简单:
import ConfigParser

config = ConfigParser.ConfigParser()

# set a number of parameters
config.add_section("book")
config.set("book", "title", "the python standard library")
config.set("book", "author", "fredrik lundh")

config.add_section("ematter")
config.set("ematter", "pages", 250)

# write to file
config.write(open('1.ini', "w"))

×××××××××××××××××××××××××××××××××××××××××
修改也不难(添加内容):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser

config = ConfigParser.ConfigParser()

config.read('1.ini')

a = config.add_section("md5")

config.set("md5", "value", "1234")

config.write(open('1.ini', "r+"))     #可以把r+改成其他方式,看看结果:)

修改内容:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser

config = ConfigParser.ConfigParser()

config.read('1.ini')

config.set("md5", "value", "kingsoft")    #这样md5就从1234变成kingsoft了

config.write(open('1.ini', "r+"))
删除部分(懒得写了,自己看文档):

remove_option( section, option)

Remove the specified option from the specified section. If the section does not exist, raise NoSectionError. If the option existed to be removed, return True; otherwise return False. New in version 1.6.

remove_section( section)

Remove the specified section from the configuration. If the section in fact existed, return True. Otherwise return False.

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wayne92/archive/2007/09/26/1802092.aspx

 

原创粉丝点击