Python (2): File

Source: Internet
Author: User

#file操作要点: Aaa.txt for Desktops

With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' R ') as F:
X1=f.read () #得到一个字符串
Print type (x1)
Print (x1)
F.seek (0,0)
Print (1)
X2=f.readline () #得到第一行, forming a string
Print type (x2)
Print (x2)
# F.seek (0,0), no this line reads all the remaining rows
Print (F.readlines ()) #得到一个列表, each line as an element

With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' w+ ') as F:
F.write (' What does You Like?\nboth ') #清空原来的重新写
F.writelines (' 123\n ') #接着写,
F.writelines (' Thank you ')
F.seek (0,0)
Print (1)
Print F.read ()

Ans

<type ' str ' >
What does it like?
Both123
Thank
1
<type ' str ' >
What does it like?

[' both123\n ', ' thank you ']
1
What does it like?
Both123
Thank

Remark: No F.wirteline

#从末尾开始写:

With open (R ' C:\Users\xuyin\Desktop\aaa.txt ', ' A + ') as F:
F.write (' See you! ')
F.seek (0,0)
Print (F.read ())

Ans

What does it like?

Both123
Thank YouSee You!

File read type:??

W: Write only

R:

w+

r+

A +: Append

After encountering binary, add ' B ': Rb,wb ...

Python (2): File

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.