Python: File read, create, append, delete, empty

Source: Internet
Author: User

One, create a new file with Python, the content is an integer from 0 to 9, each number occupies one line:
#python
>>>f=open (' F.txt ', ' W ')     # R Read Only, W writable, a append
>>>for i in Range (0,10): F.write (str (i) + ' \ n ')
.  . .
>>> F.close ()
II, File contents appended, 10 random integers from 0 to 9:
#python
>>>import random
>>>f=open (' F.txt ', ' a ')
>>>for i in Range (0,10): F.write (str (random.randint))
0,9 . .
>>>f.write (' \ n ')
>>>f.close ()
Three, the contents of the file appended, from 0 to 9 of random integers, 10 digits in a row, 10 lines:
#python
>> > Import Random
>>> f=open (' f.txt ', ' a ')
>>> for I in Range (0,10):
.  .  .      for I in Range (0,10): F.write (str (random.randint (0,9)))  
.  .  .      f.write (' \ n ')     
.  . .
>>> F.close ()
Four, direct the standard output to a file:
#python
>>> import sys
>>> sys.stdout = Open (" Stdout.txt "," W ")
>>>  ...

V. Reading and writing of documents

First, the file opens:

f = File (name[, mode[, Buffering])

Entry parameters: Name File name

mode option, String

Buffering whether buffered (0= not buffered, 1 = buffered, int number of >1 = buffer size)

Return value: File object

Mode options:

"R" opens in read-only file, exception occurs if the file does not exist

"W" opens in write mode and can only write files if the file does not exist, create the file

If the file already exists, empty it before opening the file

"RB" is opened in binary read mode, only read files, if the file does not exist, an exception occurs

"WB" opens in binary write, can only write files, if the file does not exist, create the file

If the file already exists, empty it before opening the file

"RT" opens as a text read, can only read a file, and if the file does not exist, an exception occurs

"WT" is opened as text, can only write files, if the file does not exist, create the file

If the file already exists, empty it before opening the file

"Rb+" is opened in binary read, can read, write the file, if the file does not exist, an exception will occur

"Wb+" is opened in binary write, can read, write the file, if the file does not exist, create the file

If the file already exists, empty it before opening the file

Second, close the file

F.close ()

When the file is read and written, the file should be closed.

Third, empty the contents of the file

F.truncate ()

Note: Only files opened in writable mode, such as "r+" "rb+" "W" "WB" "wb+" can perform this function.

Iv. pointer positioning and querying of files

(1) file pointer:

When the file is opened, its object is stored in F, which remembers the current location of the file so that it can perform a read, write operation, which is called a pointer to the file (a long type of bytes that starts at the head of the file).

(2) Where the file is opened:

with "R" "r+" "rb+" read mode, "W" "w+" "wb+" to open the file,

At first, the file pointer points to the file's head.

(3) Gets the value of the file pointer:

L = F.tell ()

(4) Pointer to move file

F.seek (offset, option)

option = 0 o'clock, which indicates that the file pointer is pointing from the file header to the "offset" byte.

option = 1 o'clock, which indicates that the file pointer points to the current position of the file and moves the "offset" byte backwards.

option = 2 o'clock, which means pointing the file pointer from the end of the file, moving the "offset" byte forward.

V. Reading from a file means content

1 reading of text files (files opened in "RT" mode)

s = F.readline ()

Return value: S is a string that reads a line from a file with a line terminator.

Description: (1) If Len (s) =0 indicates the end of the file

(2) If it is the last line of the file, there may not be a line terminator

22 binary files (files opened in "RB", "rb+", "wb+") read

s = F.read (n)

Description: (1) If Len (s) =0 indicates the end of the file

(2) After the file has been read, the pointer of the file moves the Len (s) byte backwards.

(3) If the track is bad, an exception occurs.

Six, write a string to the file

F.write (s)

Parameter: s the string to write

Description: (1) After the file has been written, the pointer of the file moves the Len (s) byte backwards.

(2) If the track is bad, or the disk is full, an exception occurs.

Return value: S is a string that reads content from a file

Vii. deletion of files

Import OS

Os.remove (file)

Python: File read, create, append, delete, empty

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.