"Python" file read and write operations

Source: Internet
Author: User

Python files read and write a bit like PHP file read and write. PHP file read and write already in the "PHP" let Notepad become your control variable of the console (click the Open link) said, below with a small example of Python file read and write.

In the F disk originally did not have f:\ hello. txt This file, we want to put the user in the Python console input content, get this file up. This program in Eclipse Pydev compiled by no garbled, Windows console cmd garbled problem please refer to "Python" to traverse the directory of all folders and file path, output Chinese garbled problem (click to open the link). The program code is as follows:

#-*-coding:utf-8-*-import os; #写操作fileName = "f:\ hello. txt" filecontent=raw_input ("The content you entered will be appended to" +filename); Fp=open ( Filename.decode (' Utf-8 '). Encode (' GBK '), "A +"), Fp.write (filecontent);p rint "What you entered:" +filecontent;print "appended to:" + Filename;fp.close ();p rint, #读操作fileSize =os.path.getsize (Filename.decode (' utf-8 '). Encode (' GBK '); Fp=open ( Filename.decode (' Utf-8 '). Encode (' GBK '), "A +");p rint "FileName now has a file size of:" +str (fileSize) + "B";p rint "Now" +filename+ " is: "Print Fp.read (fileSize). Decode (' Utf-8 '); Fp.close ();

The results are as follows:


Here, the main description of several file operation methods:

1. The system's intrinsic class OS can get the file size: os.path.getsize (filename), where filename is the path.

You can see that the path above is passed by me. Decode (' Utf-8 '). Encode (' GBK ') is first encoded as utf-8, then decoded to GBK, to adapt to the system encoding, otherwise garbled.

Primarily, the system's handling of file names is inconsistent with Python's encoding of file names. Even if you put #-*-coding:utf-8-*-in the beginning,

However, the processing of the contents of the file does not have to be so. Python's coding is weird.

2. Open method The second parameter of the code file is read in a way. Although Python is like the C language, there are three main classes of a+,w+,r+ available for reading.

W, open in write mode,
A, open in Append mode (starting with EOF, creating a new file if necessary)
r+, open in read-write mode
w+, open in read/write mode (see W)
A +, open in read/write mode (see a)
RB, opens in binary read mode
WB, opens in binary write mode (see W)
AB, opened in binary append mode (see a)
rb+, open in binary read/write mode (see r+)
wb+, open in binary read/write mode (see w+)
ab+, open in binary read/write mode (see A +)

However, experienced in-person testing, found whether it is read and write operations, all with a + will not be wrong, do not know why, anyway, PHP is playing, I try in Python, a beginning with r+ to read the document old, the results of a unified A + but can, very strange.

3, finally, all programming languages are the same, whether it is a file or database, people walk with the door, self-closing the connection method, are not closed by default ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Python" file read and write 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.