file in python

First, print the project path

import them
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
print "Project path is",PROJECT_ROOT

2. Open the file and read line by line

open()

def testopenFile(fileName):
    import them
    PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
    fo = open(fileName)
    print "Project path is",PROJECT_ROOT
    print "The file name is", fo.name 
    print "The number of lines in the file is: ", len(fo.readline())
    for line in fo.readlines():
        line = line.strip()
        print "data is: %s" %(line)
    fo.close()

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566078&siteId=291194637