introduction
.read([size]) Method
.readlines() Method
introductionwith open() as and open() It's all open , Not yet Read in file
hypothesis test.fa The content of is shown in the figure below :
.read([size]) MethodACGACGTAGCGTAGCTACGAT
CAGCGACGAGCTAGCGACGA
read([size]) Method reads from the current location of the file size Bytes , If there is no parameter size, It means that the file is read until the end of the file , It returns a string object .
with open('test.fa') as fa: f = fa.read() print(f) print(type(f)) print('------') f = f.split('\n') print(f[0])
Return results
.readlines() MethodCGACGTAGCGTAGCTACGAT
CAGCGACGAGCTAGCGACGA
<class 'str'>
------
CGACGTAGCGTAGCTACGAT
readlines() Method to read all lines , Save in a list (list) variable , Each line as an element , Be similar to fa.read().split('\n') Result .
readlines Read all lines , And output in list form , You can use subscripts to locate each line
with open('test.fa') as fa: f = fa.readlines() print(type(f)) print(f[0]) print('------') print(f[1])##2. readlines() Method
Return results
<type 'list'>
CGACGTAGCGTAGCTACGAT
------
CAGCGACGAGCTAGCGACGA
That's all python File read read And readlines The two methods use detailed content , More about python File read read readlines Please pay attention to other relevant articles of software development network !