本文共 4763 字,大约阅读时间需要 15 分钟。
目录
f = open('test2.py', 'r')t1 = f.read()print(t1)f.close()
运行结果为:
In [10]: runfile('F:/spyder_workspace/test2.py', wdir='F:/spyder_workspace')f = open('test2.py', 'r')t1 = f.read()print(t1)f.close()
用read(10)读取十个字节的数据:
f = open('test2.py', 'r')t1 = f.read(10)print(t1)f.close()
结果为:
In [14]: runfile('F:/spyder_workspace/test2.py', wdir='F:/spyder_workspace')f = open('
用readline()读取数据:
f = open('test2.py', 'r')t1 = f.readline()print(t1)f.close()
结果为:
In [15]: runfile('F:/spyder_workspace/test2.py', wdir='F:/spyder_workspace')f = open('test2.py', 'r')
用readlines()读取数据:
f = open('test2.py', 'r')contents = f.readlines()i = 1for t in contents: print(i, '\t', t) i += 1f.close()
In [16]: runfile('F:/spyder_workspace/test2.py', wdir='F:/spyder_workspace')1 f = open('test2.py', 'r')2 3 contents = f.readlines()4 i = 15 for t in contents:6 print(i, '\t', t)7 i += 18 f.close()
with open('test2.py', 'r') as f: contents = f.readlines() i = 1 for t in contents: print(i, '\t', t) i += 1
In [18]: runfile('F:/spyder_workspace/test2.py', wdir='F:/spyder_workspace')1 with open('test2.py', 'r') as f:2 contents = f.readlines()3 i = 14 for t in contents:5 print(i, '\t', t)6 i += 1
还可以指定所读取文件采用的编码方式:
f = open('test.txt', 'r', ecoding = 'utf-8')
f = open('test.txt', 'rb')
f = open('doc.txt', 'w', encoding = 'utf-8')contents = ['\n','we ', 'are ', 'the ', 'best ']f.writelines(contents)f.close()
close()
方法时,操作系统才保证把没有写入的数据全部写入磁盘。忘记调用close()
的后果是数据可能只写了一部分到磁盘,剩下的丢失了。所以,还是用with
语句更保险。示例一:
# -*- coding: utf-8 -*-from io import StringIOs = StringIO('hello world!')content = s.read()print(content)
运行结果为:
hello world!
示例二:
# -*- coding: utf-8 -*-from io import StringIOs = StringIO()s.write('hello world!\n')s.write('we are the best!')print(s.getvalue())
运行结果为:
hello world!we are the best!
StringIO使得我们能够像操作文件一样操作str,而如果要操作二进制数据就要使用BytesIO,其用法和StringIO是类似的,只不过操作的是二进制数据:
from io import BytesIObs = BytesIO()bs.write('hello world!'.encode('utf-8'))print(bs.getvalue())
python内置了os、os.path等模块实现对文件和目录的操作,其实就是对操作系统提供的系统调用接口进行了封装。
首先需要导入os模块:
import os
使用os.path.abspath(path)来查看path的绝对路径,'.'表示当前目录,'..'表示上一级目录:
In [105]: os.path.abspath('.')Out[105]: 'F:\\spyder_workspace'
使用os.path.join(path1,path2)来将生两个路径合并成一个新路径名,虽然可以直接进行字符串合并,但这里不推荐:
In [113]: dir1 = os.path.join('F:\spyder_workspace', 'dir1')In [114]: dir1Out[114]: 'F:\\spyder_workspace\\dir1'
使用os.mkdir(path)来创建一个目录:
In [117]: os.mkdir(dir1)
使用os.rmdir(path)来删除目录:
In [118]: os.rmdir(dir1)
使用os.path.split(path)和os.path.splitext(path)来拆分路径,前者能将文件所在目录的路径和文件名(包括扩展名)拆分开,后者是将文件名和扩展名拆分开:
In [117]: os.mkdir(dir1)In [118]: os.rmdir(dir1)In [119]: filename = 'F:\spyder_workspace\test2.py'In [120]: t1 = os.path.split(filename)In [121]: t1Out[121]: ('F:\\', 'spyder_workspace\test2.py')In [123]: t2 = os.path.splitext(filename)In [124]: t2Out[124]: ('F:\\spyder_workspace\test2', '.py')
使用os.listdir(path)可以列出path下的文件名:
In [131]: os.listdir('.')Out[131]: ['$Recycle.Bin', 'AMTAG.BIN', 'bootmgr', 'BOOTNXT', 'Config.Msi', 'Documents and Settings', 'hiberfil.sys', 'Intel', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'swapfile.sys', 'System Volume Information', 'Temp', 'Users', 'Windows']
还有很多的函数如os.rename()对当前目录的文件重命名,os.remove()删除文件、os.path.isdir()、os.isfile()、os.chdir()等等。
序列化是指将对象转化为可存储或传输的字节序列的过程,其相反过程称为反序列化,将对象序列化后就能够存储到磁盘或通过网络进行传输。在python中通过pickle进行序列化。
import pickled = dict(name = 'Mike', age = 20, major = 'computer science')f = open('doc.txt', 'wb')#bt = pickle.dumps(d) #将对象d序列化pickle.dump(d, f) #将对象d序列化后写入文件f.close()
使用pickle.load()从file-like object中反序列化结果,或直接使用pickle.loads()直接将字节数据反序列化:
import picklef = open('doc.txt', 'rb')d = pickle.load(f) #从文件中反序列化出对象print(d)f.close()
转载地址:http://rmhzb.baihongyu.com/