python 文件

输入

1.

fname='in.txt'
fr=file(fname,'r')
while k>0:
    line=fr.readline()
    if len(line)==0:
        break
fr.close()

2.

url="in.txt"
f = open(url,"rb").read()  #这个不用关闭
words = wtl32(f)

 3.

    fr=open('my_test.out','r')
    line=fr.readline()
    fr.close()

4. 输入并按单词分句

def readfile(fname='in.txt'):
    fr=file(fname,'r')
    lines=[]
    while True:
        line=fr.readline()
        if len(line)==0:
            break
        one_line=re.findall(r'[0-9]+',line)
        lines.append(one_line)
    fr.close()
    return lines #//返回以单词为单位的二维数组

 5.

file_path = 'xx.dat'
with open(file_path, 'rb') as f:
    for line in f:
        line=line.decode()
        words=line.split('::')

输出

1.

out_f = open("out.txt","wb")
for j in y:
        print >>out_f,j[0],j[1]# 输出到文件out_f
out_f.close()

 ============================

获取当前路径下的所有文件

import os
filenames=os.listdir(os.getcwd())
for name in filenames:
    print name
原文地址:https://www.cnblogs.com/phoenix13suns/p/2817835.html