python read the file row

·read

1

2

3

4

file = open("sample.txt")

for line in file:

        pass # do something

file.close()

   

Remove line breaks

1

2

3

for line in file.readlines():

        line=line.strip('\n')

        pass # do something

   

·write:

1

2

3

4

line= "aaaaaaaaadddd"

file = r'/root/l0626/test.txt'

with open(file, 'a+') as f:

f.write(line+'\n')

   

   

· About parameters

'R': Read

'W': write

'A': Append

'R +' == r + w (readable and writable, if the file exists on the error (IOError))

'W +' == w + r (read and write, the file is created if present)

'A +' == a + r (writable can be added, if there is a file created)

Correspondence, if it is a binary file, you can add a thousand million b:

'RB' 'we' 'baby' 'RB +' 'I +' bitter '+'

 

Guess you like

Origin www.cnblogs.com/liuxia912/p/11918915.html