Python file operations

Source: Internet
Author: User

Python file operations:

Opening a file; Open function

Open is available in three different ways

1,r (Read-only) is also the default file open mode in Python

2,w (write-only) write-only mode, more dangerous, will overwrite the current file contents

3,a (Append) append mode, append content to File

Read file:

There are three ways to read

Read () reads the entire file content every time, and assigns a value to a variable if the file is larger than memory. It's not going to happen this way.

ReadLine ()

ReadLines ()

The latter two are very similar, the only difference being that Readines is to read the entire file at once, to parse the file content into a list of rows that can be used with Python for. In.. Structure for processing.

While ReadLine reads only one line at a time, it is much slower than readlines and will only be used if there is not enough memory to read the entire file at once. ReadLine ()

The most basic open File script:

#!/usr/bin/env python
A =open ('/etc/passwd ') opens the/etc/passwd file in a read way
b =a.read () reads the entire file contents and assigns a value to the variable b
A.close () Close file
Print (b) printing variable b

To read the specified row through the for _ in range () statement

#!/usr/bin/env python
File = open ('/etc/passwd ', ' R ')
For a in range (4): Set A For loop, number of cycles =4
Print (File.readline ()) line reads, reads 4 times (4 rows)
File.close closing files

Read a file in a list

#!/usr/bin/env python
File = open ('/etc/passwd ', ' R ')
List = File.readlines () writes the result of ReadLines to list
File.close closing files
A=list[0:3] Cut List A, keep only the first 3 index
Print (a)

To perform a write operation on a file:

Write,writelines

Write (String)

Write is written directly to the string

#!/usr/bin/env python
File=open (' Test.txt ', ' W ')--if the file already exists, it will overwrite the original content
File.write (' This is a test File,\nand Alben is very hard study\n ')
File.close ()

Writelines (list)

Writelines is a list of files that can be written to a file

#!/usr/bin/env python
a=["apple\n"]
b=["orange\n"]
c=["banana\n"]
File=open (' Lines.txt ', ' W ')
File.writelines (a)
File.writelines (b)
File.writelines (c)
File.close

Note that when you write a list to a file, Writelines does not support wrapping, so you must write line breaks in the list

#!/usr/bin/env python
File=open ("UserDB", "A +")
File.write ("hello\n")

File operations for Python

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.