Python File Operator
基本文件操作
In [1]: f = open('test.txt')
In [2]: f.read()
Out[2]: 'abeen....2010-10-21 20:28:10.303459\nabeen....2010-10-21 23:17:51.120509\nabeen....2010-10-21 23:18:13.574263\n'
In [8]: f.readline()
Out[8]: 'abeen....2010-10-21 20:28:10.303459\n'
//读取所有行,返回数组列表
In [17]: l =f.readlines()
In [18]: l
Out[18]:
['abeen....2010-10-21 20:28:10.303459\n',
'abeen....2010-10-21 23:17:51.120509\n',
'abeen....2010-10-21 23:18:13.574263\n']
//调整位置
In [16]: f.seek(0)
In [20]: f = open('test.txt', 'w')
In [22]: f.write("this is a test")
In [25]: f.close()
In [26]: f = open('test.txt', 'r')
In [27]: f.read()
Out[27]: 'this is a test'
In [28]: f.close()
//追加内容
In [30]: f = open('test.txt', 'a')
In [31]: f.write("this is append'\n'")
In [32]: f.close()
In [33]: f = open('test.txt', 'r')
In [34]: f.read()
Out[34]: "this is a testthis is append'\n'"
Python在读取一个文件时,会记住其在文件中的位置
In [35]: f.seek(0)
In [36]: f.read(3)
Out[36]: 'thi'
In [37]: f.tell()
Out[37]: 3
有时可能需要以二进制方式读写文件,比如图片和可执行文件。此时,只要在打开文件的方式参数中增加一个“b”即可
In [40]: f = open('test.txt', 'wb')
In [41]: f.write('asdfasdfasd')
In [42]: f.close()
In [46]: f = open('test.txt', 'rb')
In [47]: f.read()
Out[47]: 'asdfasdfasd'
In [48]: f.close()
使用“os.path”来获取基本信息
In [1]: ls
file_test.py test.txt
In [2]: fils_status = "test.txt"
In [5]: import os
In [6]: print os.path.isdir(fils_status)
False
In [9]: print os.path.isfile(fils_status)
True
In [10]: print os.path.islink(fils_status)
False
In [11]: print os.path.ismount(fils_status)
False
目录操作
列出目录下内容
In [15]: for filename in os.listdir('file'):
....: print filename
file_test.py
test.txt
创建目录
In [17]: os.mkdir('abeen')
In [18]: ls
abeen/ file_test.py test.txt
删除目录
In [19]: os.rmdir('abeen')
In [20]: ls
file_test.py test.txt
多级目录
In [23]: os.makedirs('l/abeen/xiao/sb')
In [24]: ls
file_test.py l/ test.txt
In [36]: os.removedirs('l/abeen/xiao/sb')
In [37]: ls
file_test.py test.txt
对指定类型文件进行操作,用fnmatch
In [33]: for file_name in os.listdir('./'):
....: if fnmatch.fnmatch(file_name, '*.txt'):
....: print file_name
test.txt
In [34]: l = ["aa", "aa.txt", "abeen.img", "shanshan.txt"]
In [35]: l
Out[35]: ['aa', 'aa.txt', 'abeen.img', 'shanshan.txt']
In [36]: fnmatch.filter(l,'*.txt')
Out[36]: ['aa.txt', 'shanshan.txt']
In [37]: fnmatch.filter(l,'[a]*')
Out[37]: ['aa', 'aa.txt', 'abeen.img']
创建“虚拟”文件
可以使用“StringIO”模块来创建文件并将其保存在内存中
StringIO
In [39]: import StringIO
In [40]: f = StringIO.StringIO("this is a test")
In [42]: f.read()
Out[42]: 'this is a test'
In [43]: dir(f)
Out[43]:
['__doc__',
'__init__',
'__iter__',
'__module__',
'buf',
'buflist',
'close',
'closed',
'flush',
'getvalue',
'isatty',
'len',
'next',
'pos',
'read',
'readline',
'readlines',
'seek',
'softspace',
'tell',
'truncate',
'write',
'writelines']
In [45]: f.close()
cStringIO 应该比 StringIO速度快些吧
In [1]: import cStringIO
In [2]: f = cStringIO.StringIO("test by abeen \n test by abeen")
In [3]: f.readline()
Out[3]: 'test by abeen \n'
In [4]: f.readline()
Out[4]: ' test by abeen'
In [5]: f.seek(0)
In [6]: f.read()
Out[6]: 'test by abeen \n test by abeen'
In [7]: dir(f)
Out[7]:
['__class__',
'__delattr__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__iter__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'close',
'closed',
'flush',
'getvalue',
'isatty',
'next',
'read',
'readline',
'readlines',
'reset',
'seek',
'tell',
'truncate']
In [8]: f.close()