ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 软件工程 >> python爬虫 python文件操作学习笔记

python爬虫 python文件操作学习笔记

来源:网络整理     时间:2017-11-19     关键词:python爬虫

本篇文章主要介绍了"python爬虫 python文件操作学习笔记",主要涉及到python爬虫方面的内容,对于软件工程感兴趣的同学可以参考一下: #文件操作:读:f = open("/Users/zhouhaijun/python/01.py","r")x = f.read()print x写:f = o...

#文件操作:

读:

f = open("/Users/zhouhaijun/python/01.py","r")

x = f.read()

print x

写:

f = open("/Users/zhouhaijun/python/file_01.py","wb")

f.write("ok")

f.close()

读:

f = open("/Users/zhouhaijun/python/file_01","r")

text = f.read() #text = f.read(100)

print text

读取一行:

print f.readline()  #按行读取

接下来的函数是拷贝文件,一次读写五十个字符。第一个参数是源文 件名,第二个参数是新文件名 

def copyFile(oldfile, newfile):

f1 = open(oldfile,"r")

f2 = open(newfile,"w")

while 1:

text = f1.read(50)

if text == "":

break

f2.write(text)

f1.close()

f2.close()

return

接下来的例子是行处理程序,函数filterFile拷贝一个文件,同时将旧

文件中不是以“#”开头的行写入新文件中:

def fileterFile(old, new):

sfile = open(old, "r")

dfile = open(new, "w")

while 1:

text = sfile.readline()

if text =="":

break

elif:

text[0] = "#":

continue

else:

dfile.write(text)

sfile.close()

dfile.close()

以上就介绍了python爬虫 python文件操作学习笔记,包括了python爬虫方面的内容,希望对软件工程有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_4501664.html

上一篇 你所不知道的 下一篇 excel写入笔记

python爬虫相关图片

python爬虫相关文章