a13698822086 发表于 2017-5-1 07:13:08

【总结】Python 文件操作

1. 几种常用的读取文件的方式:



(1) 读取文件

file_object = open('thefile.txt')
try:
for line in file_object:
    'process line'
    pass
finally:
file_object.close()


(2)一次读取所有的内容,并保存到一个大字符串中
#read the file as text
all_the_text = open('thefile.txt').read()


(3)以二进制格式读取
#read the file as binary data
all_the_data = open('abinfile', 'rb').read()




(4)
file_object = open('thefile.txt')
try:
all_the_text = file_object.read()
finally:
file_object.close()


(5) 逐行读取文本文件内容,并且将读取的数据保存到一个字符串列表中:
list_of_all_the_lines = file_object.readlines()



这样读取的每行末尾带有"\n"符合,如果不需要这个结束符,可以采用以下四种方式:


list_of_all_the_lines = file_object.read().splitlines()


list_of_all_the_lines = file_object.read().split('\n')


list_of_all_the_lines =



for line in file_object:
line = line.rstrip('\n')



(6)
如果你需要去除每行末尾的空白符:
for line in file_object:
line = line.rstrip()



(7)
依次读取二进制文件的100个字符,一直到文件末尾。
file_object = open('abinfile', 'rb')
try:
while True:
    chunk = file_object.read(100)
    if not chunk:
      break
    do_something_with(chunk)
finally:
    file_object.close()



(8)
依次读取二进制文件的若干个字符,一直到文件末尾。
def read_file_by_chunks(filename, chunksize=100):
file_object = open(filename, 'rb')
while True:
    chunk = file_object.read(chunksize)
    if not chunk:
      break
    yield chunk
file_object.close()





for chunk in read_file_by_chunks('abinfile'):
do_something_with(chunk)





2.
写入文件:


(1) 将一个长字符串写入文件或者二进制文件:


写入文本到文本文件
open('thefile.txt', 'w').write(all_the_text)


写入数据到二进制文件
open('abinfile','wb').write(all_the_data)



(2)
file_object = open('thefile.text', 'w')
file_object.write(all_the_text)
file_object.close()



(3)
如果写入的数据是在一个大字符串中,而是在一个字符串列表或者序列中,应该使用writelines方法(同样适用于二进制文件写入)


file_object.writelines(list_of_text_strings)
open('abinfile', 'wb').writelines(list_of_data_strings)



(4)




3.
获取指定路径下的文件列表


>>> import glob
>>> glob.glob('D:/Personal/Music/Fun/*.mp3')
['D:/Personal/Music/Fun\\All Alone.mp3',
'D:/Personal/Music/Fun\\All Alright.mp3',
'D:/Personal/Music/Fun\\All The Pretty Girls.mp3',
"D:/Personal/Music/Fun\\At Least I'M Not As Sad (As I Used To Be).mp3",
'D:/Personal/Music/Fun\\Barlights.mp3',
'D:/Personal/Music/Fun\\Be Calm.mp3',
'D:/Personal/Music/Fun\\Benson Hedges.mp3',
'D:/Personal/Music/Fun\\Carry On.mp3']




>>> os.listdir(" D:/Personal/Music/Fun")
['AlbumArtSmall.jpg', 'All Alone.mp3', 'All Alright.mp3', 'All The Pretty Girls.mp3', "At Least I'M Not As Sad (As I Used To Be).mp3", 'Barlights.mp3', 'Be Calm.mp3', 'Benson Hedges.mp3', 'Carry On.mp3']



>>> glob.glob('D:/Personal/Music/Fun/A*.mp3')
['D:/Personal/Music/Fun\\All Alone.mp3',
'D:/Personal/Music/Fun\\All Alright.mp3',
'D:/Personal/Music/Fun\\All The Pretty Girls.mp3',
"D:/Personal/Music/Fun\\At Least I'M Not As Sad (As I Used To Be).mp3"]





>>> glob.glob(' D:/Personal/Music/*/A*.mp3'')
一个通配符是 "*.mp3" (用于匹配 用于匹配 用于匹配 用于匹配 用于匹配 .mp3 文件 );
另一个通配符是子目录名本身,用于匹配 c:\music 中的所有子目录。
页: [1]
查看完整版本: 【总结】Python 文件操作