Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8716643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:08:02+00:00 2026-06-13T06:08:02+00:00

Runtime Environment: Python 2.7, Windows 7 NOTE:I am talking about the encoding of the

  • 0

Runtime Environment: Python 2.7, Windows 7

NOTE:I am talking about the encoding of the file generated by the PYTHON source code(NOT talking about the PYTHON source file’s encoding), the encoding declared at the top of the PYTHON source file DID agree with the encoding in which the PYTHON source file was saved.

When there are no non-ascii characters in the string(content = 'abc'), the file(file.txt, NOT the PYTHON source file) is saved in ANSI encoding after fp.close(), the PYTHON file’s(and it is saved in ANSI encoding format) content is as below:

## Author: melo
## Email:prevision@imsrch.tk
## Date: 2012/10/12
import os

def write_file(filepath, mode, content):
    try:
        fp = open(filepath, mode)
        try:
            print 'file encoding:', fp.encoding
            print 'file mode:', fp.mode
            print 'file closed?', fp.closed
            fp.write(content)
        finally:
            fp.close()
            print 'file closed?', fp.closed
    except IOError, e:
        print e


if __name__ == '__main__':
    filepath = os.path.join(os.getcwd(), 'file.txt')
    content = 'abc'
    write_file(filepath, 'wb', content)

but when there are some non-ascii characters in the string(content = 'abc莹'), the file(file.txt) will be saved in UTF-8 encoding after fp.close(), although I declared the encoding at the top of the PYTHON source file(not file.txt) with #encoding=gbk. At this time, the PYTHON source file’s content is as below:

# -*- encoding: gbk -*-
## Author: melo
## Email:prevision@imsrch.tk
## Date: 2012/10/12
import os

def write_file(filepath, mode, content):
    try:
        fp = open(filepath, mode)
        try:
            print 'file encoding:', fp.encoding
            print 'file mode:', fp.mode
            print 'file closed?', fp.closed
            fp.write(content)
        finally:
            fp.close()
            print 'file closed?', fp.closed
    except IOError, e:
        print e

if __name__ == '__main__':
    filepath = os.path.join(os.getcwd(), 'file.txt')
    content = 'abc莹'
    write_file(filepath, 'wb', content)

Is there any proof that it behaves like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:08:03+00:00Added an answer on June 13, 2026 at 6:08 am

    A file is saved in the encoding you save it in. A source file is saved in the encoding you save it in. They don’t have to be the same, they just should be declared.

    Per your other question, I assume you are using Notepad++ and when you open file.txt you find that Notepad++ thinks the file is encoded in UTF-8 without BOM. This is an incorrect guess by Notepad++. Select the Chinese GB2312 character set and the file will display properly.

    Unless given a hint by a byte order mark (BOM) or some other metadata or told by the user, programs have no idea what encoding a file is in.

    A correct Python program would do these things:

    1. Declare the encoding of the source file if non-ASCII characters are used in the source file.
    2. Use Unicode strings for all text.
    3. Encode the Unicode strings when output to a binary stream such as a file.
    4. Decode incoming text data to Unicode when read from a binary stream.
    5. (optional) Use an encoding with a byte-order mark so editors know the file encoding.

    Example:

    # encoding: utf-8
    import codecs
    with codecs.open('file.txt','wb',encoding='utf-8-sig') as f:
        f.write(u'abc莹')
    

    You should now see in Notepad++ that file.txt is detected as encoded as ‘UTF-8’ (with BOM) and display the file properly.

    Note that you can save the file in ‘ANSI’ (GBK on your system) if you declare the encoding as gbk and it will still work because Unicode strings were used.

    Actually, your system probably is code page 936 (cp936) instead of GBK. They aren’t precisely the same. Better to use a Unicode encoding like UTF-8 or UTF-16 which can represent all Unicode characters accurately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code is not compiling in Java: java version 1.6.0_24 OpenJDK Runtime Environment
The code has a runtime dependency which is not available in our development environment
Can someone advice which is the recommended version of Java Runtime Environment for Windows
Every time I run my code in Python IDLE development environment, I get a
I want to know about Binary Runtime Environment for Wireless tutorial(Brew). Any one provide
I wonder if there is a 'local runtime environment' for hosting Javascript code, that's
I'm making a Google AppEngine Application. Does the Python 2.5.2 runtime environment follow the
My runtime environment is still on JDK1.4 but I like the Solr features related
What is the WebORB for PHP runtime environment? It supposedly connects Flex, Flash, AJAX
I have a Java application which is using a certain Java Runtime Environment. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.