Iterating through a file instance in Python using the For,while loop

Source: Internet
Author: User
Tags readline

traversing a file with a for loop

Open File

Open

r: Open in read mode

w: Open in write mode

A: Open in Append mode

r+: Open in read-write mode

w+: Open in read/write mode (see W)

A +: Open in read/write mode (see a)

RB: Open in binary read mode

WB: Opens in binary write mode (see W)

AB: Open in binary append mode (see a)

rb+: Open in binary read/write mode (see r+)

wb+: Open in binary read/write mode (see w+)

ab+: Open in binary read/write mode (see A +)


View Help:

Open (...)

Open (name[, mode[, buffering]), file object

Open a file using the file () type, returns a file object. The

Preferred to open a file. See file.__doc__ for further information.

(END) ... skipping ...


[Email protected] ~]# Cat/tmp/1.txt

1111

[Email protected] ~]#

Read-only mode open:

in [+]: Open ('/tmp/1.txt ')

OUT[26]: <open file '/tmp/1.txt ', mode ' R ' at 0x20860c0>

in [+]: FD = open ('/tmp/1.txt ')

in [+]: FD

OUT[28]: <open file '/tmp/1.txt ', mode ' R ' at 0x20861e0>

In []: type (FD)

OUT[29]: File

Open in write mode:

in [+]: FD = open ('/tmp/1.txt ', ' W ')

in [+]: fd.write (' 2222\n ')

In [approx]: Fd.close ()

[Email protected] ~]# Cat/tmp/1.txt

2222

[Email protected] ~]#

Open in Append mode:

in []: FD = open ('/tmp/1.txt ', ' a ')

in [+]: fd.write (' 3333\n ')

In [approx]: Fd.close ()

[Email protected] ~]# Cat/tmp/1.txt

2222

3333

[Email protected] ~]#

Read ():

in [+]: Fd.read ()

OUT[41]: ' 2222\n3333\n '

In [All]: Fd.read ()

OUT[42]: "

in [+]: Fd.readline ()

OUT[49]: ' 2222\n '

in [[]: Fd.readline ()

OUT[50]: ' 3333\n '

In [Wuyi]: Fd.readline ()

OUT[51]: "

In [52]:


Read () and ReadLine () return a string:

ReadLines () returns the list:

in [*]: FD = open ('/tmp/1.txt ')

in [+]: Fd.readlines ()

OUT[53]: [' 2222\n ', ' 3333\n ']

Script:

#!/usr/bin/python

FD = open ('/tmp/1.txt ')

For line in FD:

Print line,

Fd.close ()


[email protected] 20171228]# python read_file.py

2222

3333

[Email protected] 20171228]#

Traversing a file using a while loop

Script:

#!/usr/bin/python

FD = open ('/tmp/1.txt ')

While True:

line = Fd.readline ()

If not line:

Break

Print line,

Fd.close ()


[email protected] 20171228]# python read_fi_while.py

2222

3333

[Email protected] 20171228]#


With open file:

#!/usr/bin/python

With open ('/tmp/1.txt ') as FD:

While True:

line = Fd.readline ()

If not line:

Break

Print line,


Iterating through a file instance in Python using the For,while loop

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.