python对文件的独操作有哪几种?请详细说明每种方法(Python读取文件内容的方法有几种)

2261℃ ANGELO

python对文件的独操作有哪几种?请详细说明每种方法(Python读取文件内容的方法有几种)

Python读取文件内容的方法有几种

filename=open('i:\\install\\test.txt','r+')#读取xx路径xx文件;r+代表的是读写并存方式 print filename.read()#读取所有的文件

关于Python的文件操作问题,求大神们给出具体代码~ 感激不尽~

#!/usr/bin/env python

#-*- coding: utf-8 -*-

from __future__ import with_statement

import re

values = {}

lines = []

txt_file = 'data.txt'

out_file = 'result.txt'

data_re = repile(ur'^(\w)\^(\w)\s+.+?\s+(.+?)\s*$')

with open(txt_file, 'rU') as f:

for line in f:

if '^' in line:

temp = data_re.findall(line)

if temp != []:

name1, name2 = temp[0][:2]

if name1 != name2:

value = float(temp[0][-1])

name = '%s^%s' % (name1, name2)

if name not in values or values[name] < value:

values[name] = value

lines.insert(0, line.strip())

with open(out_file, 'w') as f:

f.write('\n'.join(lines[:3]))

python在代码中如何文件操作

虽然是个旧贴,但看到没有一个好的回答,怕后面像我一样的新手被误导,所以重新回帖一个使用python 的os模块import ospath = '/tmp/tt.c'if os.path.exists(path):&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;line_one = file(path, 'r').readline()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;print line_oneos.path.exists()是python提供的标准测试文件是否存在的方法

python 文件操作,求大神

直接的文本方式读确实是把值当成字符串,可以在读入后进行转换。对于列表,也可以用对象持久化的方式进行保存成文件和读入,有相应的库支持,也比较简单。