Python file operations

Source: Internet
Author: User

Python implements the operation of the file:

You need to open a file before reading it:

fileobj = open (filename, mode)

Explanation of the Open () call parameter:

* Fileobj is the file object returned by open ();

* filename is the string name of the file;

* Mode is a string that indicates the file type and operation

Mode:

* R Read-only mode (default)

* W Write-only mode (unreadable, non-existent, newly created; Overwrite new content if present;)

* A append mode (readable, non-existent then newly created; only append content;)

"+" means reading and writing a file at the same time:

* r+ can read and write files (readable; writable; can be appended)

* w+ Write Read

* A + with a

* b stands for binary files

Eg: Write a file

Conten = "Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested. "

Fout = open ("File.txt", "W")
Fout.write (Conten)
Fout.close ()

If the source string is larger, the data can be chunked until all characters are written:

Fout = open ("File.txt", "W")
size = Len (conten)
Offset = 0
Chunk = 100
While True:
If offset > Size:
Break
Fout.write (Conten[offset:offset+chunk])
Offset + = Chunk
Fout.close ()

Use Read (), ReadLine (), ReadLines () reading the file:

The read () function without parameters reads all the contents of the file at one time, noting that 2G files use the same size memory when they are read into the file

Fin = open ("file.txt", "R")
ct = Fin.read ()
Fin.close ()
Print (CT)

You can set the maximum number of read-in characters to limit the size that the read () function returns at one time.

ct = ""
Chunk = 100
Fin = open ("file.txt", "R")
While True:
fragment = Fin.read (chunk)
If not fragment:
Break
CT + = Fragment
Fin.close ()
Print (CT)

ReadLine () each time a line is read into the file, it is stitched into its original string by appending each line:

ct = ""
Fin = open ("file.txt", "R")
While True:
line = Fin.readline ()
If not line:
Break
CT + = line
Fin.close ()
Print (CT)
Or
Fin = open ("file.txt", "R")
While True:
line = Fin.readline ()
If not line:
Break
Print (line,end= "")
Fin.close ()

When the file read is finished, ReadLine (), read () also returns an empty string, which is sentenced to false.

The function readlines () calls each time a row is read and returns a list of single-line strings:

Fin = open ("file.txt", "R")
lines = Fin.readlines ()
Fin.close ()
For line in lines:
Print (line, end= "")

To close a file using with automatically:

With open ("File.txt", "R") as Fin:
While True:
line = Fin.readline ()
If not line:
Break
Print (line, end= "")

Use Seek () to change position

function tell () returns the byte offset from the beginning of the file, and the function seek () allows you to jump to the location of the other byte offset of the file, i.e. you can skip to the specified location without reading every byte of the file from the beginning

Fin = open ("file.txt", "R")
Fin.tell ()
Print (Fin.read ())
Fin.seek (10)
Print (Fin.read ())

Python file operations

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.