Python automated test Aries-week3 file operation

Source: Internet
Author: User
Tags save file

#1, open the file
#2, read/write files
#3. Save File

Name =OpenC\\Users\lingyul\desktop\login.txt ',' w+ ',encoding=' Utf-8 ')#r如果不写路径, the default current path is taken
#打开文件有三种模式,
# W: Write mode, if you open a file that already exists in W mode, it will erase the contents of the previous file and write it again.
#R: Read mode, and the file must exist
#A: Append mode, also can only read can not write, and the file must exist, add content at the end of the file, append mode open file, then the file pointer is directly in the last line
#w +: Read-write mode
#r +: Read/write mode
#a +: Can read and write, if not this file in the current directory to create this file, append mode open file, then the file pointer directly in the last behavior

#rb/wb/ab/rb+/wb+/ab+ to open some pictures, music files in binary mode

#只要有r, the file must be present
#只要有w, the file must be emptied.
Print (Name.read ())

Name.write (' ABCdef ')
Print (Name.read ())#返回的是一个字符串
Print (Name.readline ())#返回的是一个字符串
Print (Name.readlines ())#返回的是一个列表, read only one line of content


Name.write (' Zhangyz ')

#name = File (' a.txt ') ####### #在python2中有file这种用法
#如果在打开文件时不指定模式, the default is read mode

f =Open' A.txt ',' A + ',encoding=' Utf-8 ')#追加模式打开文件, the file pointer is directly at the end of the line, can read and write, if the file is not present in the current directory to create this file
F.seek (0)
F.write (' Lingyul ')#写的是字符串
F.writelines ([' 123 ',' 456 ',' 789 '])#写的是列表
F.seek (0)
Print (F.read ())

F.seek (0)
Count=0
For lineIn F:
Print' Line%s ' is:%s '% (count,line))
Count + =1


F.truncate ()#清空文件内容

res=Open' B.txt ',' A + ')
Res1=res.read ()
Res.seek (0)
Print' ########### ', Res.read ())
New_res = Res1.replace (' Nihao ',' Nibuhao ')
Print (New_res)

Import time
FW =open ( ' Python.txt ', ' W ')
Fw.write ( ' Lilingyun ')
Fw.flush () Span style= "color: #808080; Font-style:italic "> #写完之后立马生效
Time.sleep (30) #等待30秒再执行下一步
Fw.close ()

with open ( ' Python.txt ', encoding= Utf-8 ') as fr: #文件不再用的时候文件会自动关闭
print (Fr.read ())

Python automation test Aries-week3 file operation

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.