Python---file operations

Source: Internet
Author: User

File read-write open (Encoding=utf-8) #pyton2, Python3fie () #python2r read-only w write a append write, followed by + words, is read and write, read, append read and write u put all the files inside the newline character, are changed to \ NB Binary. Read () reads all the contents of the file and returns a string. ReadLine () reads all the contents of the file, returns a list, and the contents of each row is an element of the list. ReadLine () reads a line for the in F:xxx.write ( Write a string. Writelines () write a list.seek (XX) Move the file pointer to the XX position. Tell () to see where the current pointer to the file is. Truncate () empty the file. Close () closes the file. Flush () Immediately writes the data inside the memory to the file with open (' xx) as FW:FW. ()

Import Time,os
#修改文件 with the ability to manipulate multiple files at the same time without the close file operation
With open (' A.txt ', ' R ', encoding= ' Utf-8 ') as fr,\
Open (' A.txt.bak ', ' W ', encoding= ' Utf-8 ') as FW:
Src_res=fr.read ()
Res=src_res.replace (' One ', ' two ') #把文件中的一换成二
Fw.write (RES)
Os.remove (' A.txt ')
Os.rename (' A.txt.bak ', ' a.txt ')
#os. System (' Iptables-i INPUT 1-p tcp-s 192.168.1.1-j DROP ') #将ip地址加入黑名单
# #修改文件内容, it is not advisable to get the original file content and write the modified content into the new file.
# fw=open (' A.txt.bak ', ' W ', encoding= ' utf-8 ')
# fr=open (' A.txt ', ' A + ', encoding= ' utf-8 ')
# fr.seek (0)
# Line=fr.readline ()
# line=line.replace (' One ', ' two ')
# Fw.write (line)


# open (' A.txt ', ' W ') #文件不存在则创建, will overwrite existing content
# open (' A.txt ', ' a ') #往文件里追加
# open (' A.txt ', ' R ') #以读的方式打开文件, file must exist
# Python2 open files with open () and file (), only open () in Python3

# fw=open (' A.txt ', ' W ', encoding= ' utf-8 ') #encoding默认不写的话取操作系统编码, the encoding under Windows is GBK, with Chinese words to add this parameter
# Print (fw.writable ()) #判断该文件是否可以写
# #fw. Write (' The weather is not very good today ')
# fw.writelines ([' 1 ', ' 2 ', ' 3 ') #写一个list, then write each element of the list into the file
# Fw.close ()

#fw =open (' a.txt ', ' A + ', encoding= ' utf-8 ') #encoding默认不写的话取操作系统编码, the encoding under Windows is GBK, with Chinese words to add this parameter
# ' r+ ' read-write mode, readable and writable, content will be overwritten, bad
# ' w+ ' reads, will overwrite the original file content, preferably not
# ' A + ' read and write, but not readable
#fw. Seek (0) #把文件指针移动到某个位置, 0 is the front, with a + to open the file, read the time with
#print (Fw.readline ())
#fw. Write (' This in A + mode ')
#fw. Flush () #立即把缓冲区的数据写到文件里面
#time. Sleep (5) #程序等待5秒
#print (Fw.read ())
# fw.truncate () #清空文件内容, if you open a file with a + because the file pointer is at the end, you want to combine. Seek (0) using
# Fw.close ()
#
# fw=open (' A.txt ', ' R ', encoding= ' utf-8 ') #encoding默认不写的话取操作系统编码, the encoding under Windows is GBK, with Chinese words to add this parameter
# #res =fw.read () #读取文件全部内容
# #res =fw.readline () #读取文件一行
# res=fw.readlines () #读取文件的所有内容, the return is a list
# for line in Res:
# Print (line)
# #print (RES)
# Fw.close ()

#处理大文件最好用以下方式
# fw=open (' A.txt ', ' A + ', encoding= ' utf-8 ') #encoding默认不写的话取操作系统编码, the encoding under Windows is GBK, with Chinese words to add this parameter
# #shift + TAB tab
# fw.seek (0)
# for line in FW:
# Print (line)

Python---file operations

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.