Python文件的读写
相比java而言,Python用几行代码就可以代替java十来行的代码,真的非常不错
'''
Created on 2009-9-2
@author: jiangqh
'''
# file create and write
context = '''hello world
hello china '''
f = file("hello.txt",'w')
f.write(context)
f.close()
文件创建
#use readline() read file
f = open("hello.txt")
while True:
line = f.readline()
if line:
print line
else :
break
f.close()
一行一行的读取
# read more lines
f = file("hello.txt")
lines = f.readlines()
for line in lines:
print line
多行读取
一次性全读出文件里的内容
'''
Created on 2009-9-2
@author: jiangqh
'''
f = open("hello.txt")
context = f.read()
print context
f = open("hello.txt")
context = f.read(5) #读取前五字节
print context
print f.tell() #获得当前指针的位置
context = f.read(5) #继续当前读取五位
print context
print f.tell() #获得当前指针的位置
f.close()
相关文档:
# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
page = urllib2.urlopen("http://bj.ganji.com/piao/zz_%E5%8C%97%E4%BA%AC-%E5%8D%97%E6%98%8C/20100210/")
soup = BeautifulSoup(page)
#ss = soup.findAll('a', href=re.compile(r"^/piao/100.&qu ......
Python中文件操作可以通过open函数,这的确很像C语言中的fopen。通过open函数获取一个file object,然后调用read(),write()等方法对文件进行读写操作。
1.open
使用open打开文件后一定要记得调用文件对象的close()方法。比如可以用try/finally语句来确保最后能关闭文件。
file_object = open('thefile.txt')
......
Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
>>> os. ......
url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Django.")
修改 urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib ......
解压django,然后到其目录下安装
前提是你安装好python.并将其配置到环境变量中,然后去django的压缩文修的下,执行以下倒命令
python setup.py install
1.创建project
首先我们打开cmd, 定位到希望新建工程的目录下, 任意目录均可. 然后键入如下命令:
django-admin.py startproject hello其中hello为新工程目录文件名 ......