how to use python

藏色散人
藏色散人Original
2019-10-15 11:56:5419716browse

how to use python

How to use python?

Here, I want to use python to create a new folder a on the computer desktop, and in folder a, create a txt document - b.txt, and write the following text:

你好,
  世界。

how to use python

Related recommendations: "Python Tutorial"

On the computer desktop, create a new folder named a .

And remember the absolute path in this folder, this is:

C:\Users\Administrator\Desktop\a

how to use python

Note that folder a is empty at this time.

how to use python

Open the python compiler;

The python I use is the compiler corresponding to the python3 version integrated by Anaconda: spyder.

how to use python

Write at the beginning:

#!/usr/bin/python

This is like an opening sentence.

how to use python

Since you need to output Chinese, you need to set the encoding format:

# -*- coding:utf-8 -*-

how to use python

Use python in folder a Inside, create a b.txt document:

file = open('C:/Users/Administrator/Desktop/a/b.txt','w')

'w' means that the document can be edited, which means it can be read and written.

how to use python

Write text inside:

file.write('你好,\n  世界。')

Among them, \n is the newline character.

how to use python

The overall code is as follows:

#!/usr/bin/python
# -*- coding:utf-8 -*-
file = open('C:/Users/Administrator/Desktop/a/b.txt','w')
file.write('你好,\n  世界。')

At this time, there is already a b.txt document in the a folder.

how to use python

Open this document and you can see the content as shown below.

how to use python

Notes

The saving path of this document, absolute path if needed, C:/Users/Administrator/Desktop/a ; Moreover, the slash inside is "/", not "\". Remember.

The above is the detailed content of how to use python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn