Python file operations,

Source: Internet
Author: User

Python file operations,
Note: to read a file in python, you must set the file as a variable and then read the file.

Mode

Description

R

Open the file in Read mode to read the file information.

W

Open the file in write mode and write information to the file. If the file exists, the file is cleared and new content is written.

A

Open the file in append mode (when the file is opened, the file pointer is automatically moved to the end of the file). If the file does not exist, create

R +

Open a file in read/write mode to read and write the file.

W +

Remove the file content and open the file in read/write mode.

A +

Open the file in read/write mode, and move the file pointer to the end of the file.

B

Open the file in binary mode, instead of text mode. This mode is only valid for Windows or Dos, and Unix-like files are operated in binary mode.

I. Opening and creating a file >>> F = open ('/tmp/test.txt ') >>> F = file ('/tmp/test.txt') # f = open ('/tmp/test.txt') Same (python3 discards file) >>> F. read () >>> F <Open file '/tmp/test.txt', mode 'R' at 0x7f5285181d20> #0x7f5285181d20 memory instruction 'Hello girl \ nhello boy! \ N \ nhello man! \ Nhello man! \ Nhello dear! \ Nhello bady! \ Nhello lady! ' Ii. File Reading
Step: Enable -- read -- refresh -- disable
>>> F = file ('/tmp/test.txt', 'R ') >>> F. read () # Method 1: read all content 'Hello girl \ nhello boy! \ N \ nhello man! \ Nhello man! \ Nhello dear! \ Nhello bady! \ Nhello lady! ' >>> F. readline () # Method 2: Read data row by row 'Hello girl \ N' >>> F. next () # method 3: Read row by rowRetrieve DataAfter reading the last row, no error is reported. 'Hello lady! ' >>> F. next () Traceback (most recent call last ): File "<stdin>", line 1, in <module> StopIteration >>> F. readlines ()# Method 4 : Stores the file content in the form of a list ['Hello boy! \ N', '\ n', 'Hello man! \ N', 'Hello man! \ N', 'Hello dear! \ N', 'Hello bady! \ N', 'Hello lady! '] >>> For I in open ('/tmp/test.txt'): # Method 5: for Loop, print ... Print I ... Hello python! Hello shell! Hello world!

3. File writing and disabling(Be careful not to clear the original file)

>>> F = file ('/tmp/test.txt', 'w ') >>> F. write ('Hello python! \ N ') >>> F. flush ()[Root @ demo_host1 tmp] # cat test.txt
Hello python! Using the r + mode will not be cleared first, but will replace the original file >>> F = file ('/tmp/test.txt', 'r + ') >>> F. write ('\ nhello shell! ') >>> F. flush ()[Root @ demo_host1 tmp] # cat test.txt
Hello shell! Implemented without replacement ① Use the r + Mode >>> F = file ('/tmp/test.txt', 'r + ') >>> F. read () 'Hello python! \ N' >>> F. write ('Hello shell! \ N ') >>> F. flush () ② Open a file in a mode >>> F = file ('/tmp/test.txt', 'A ') >>> F. write ('Hello world! \ N ') >>> F. flush ()[Root @ demo_host1 tmp] # cat test.txt
Hello python!
Hello shell!
Hello world!

F. writelines ()Multi-row write

1234567891011 >>> l = ['\nhello dear!','\nhello son!','\nhello baby!\n']>>> f = open('/tmp/test.txt','a')>>> f.writelines(l)>>> f.close()[root@node1 python]# cat /tmp/test.txthello girl!hello boy!hello man!hello dear!hello son!hello baby!
  As you can see, if you read the file before writing, the written data will be added to the end of the file without replacing the original file. This is caused by pointers. the pointer in r + mode is at the beginning of the file by default. If it is written directly, the source file will be overwritten. After reading the file through read, the pointer will be moved to the end of the file, and data writing will not be faulty. The a mode can also be used here.

F. seek (offset, option)

12345678910111213141516 >>> f = open('/tmp/test.txt','r+')>>> f.readline()'hello girl!\n'>>> f.readline()'hello boy!\n'>>> f.readline()'hello man!\n'>>> f.readline()' '>>> f.close()>>> f = open('/tmp/test.txt','r+')>>> f.read()'hello girl!\nhello boy!\nhello man!\n'>>> f.readline()''>>> f.close()
This example fully explains why f. read () needs to be executed before f. seek can be inserted normally when r + mode is used. (offset, option)
Option = 0 indicates that the file pointer is directed to the "offset" byte from the file header.
Option = 1 indicates that the file pointer is directed to the current position of the file, and the "offset" byte is moved backward.
Option = 2, indicating that the object pointer is directed to the end of the object and the "offset" byte is moved forward.
Offset: positive value indicates the offset to the right, and negative value indicates the offset to the left.
12345678910111213 >>> f = open('/tmp/test.txt','r+')>>> f.seek(0,2)>>> f.readline()''>>> f.seek(0,0)>>> f.readline()'hello girl!\n'>>> f.readline()'hello boy!\n'>>> f.readline()'hello man!\n'>>> f.readline()''

F. tell () Get pointer position

123456789 >>> f = open('/tmp/test.txt')>>> f.readline()'hello girl!\n'>>> f.tell()12>>> f.readline()'hello boy!\n'>>> f.tell()23

 

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.