python-files and built-in functions open (), file ()

Source: Internet
Author: User

Ternary Operations

Ternary operations, also known as trinocular operations, are shorthand for simple conditional statements:

Simple Conditional statement:

if condition established:    val=1else:    val=2

Change to ternary operation;

file ProcessingNow there is a file ' part-time student contacts ', how to view content?

1. Installing the Text editor software

2. Select the right button. Open with a text editor

3. View or Write

4. Save, close

File operations are divided into read, write, modify

First step, read. How to open a file

R Open as read (the file does not exist, the error is not provided access_mode default value)

W is opened in write mode (the file exists is emptied and does not exist)

A opens in Append mode (creates a new file if necessary) r+ open in read-write mode (see R) w+ open in read-write mode (see W) A + opens in read-write mode (see a)
F=open (file='d:/work daily/part-time student contact us. txt', mode='r', encoding= ' Utf-8 ' #以读的方式打开文件data=f.read () #接着上一步, to continue to manipulate the file, you will assign a value, open the file, save a variable, and then. Read f.close# Close the folder

Absolute path: d:/work daily/part-time student Contact Us

Relative path: The current file can be found

Chardet: Judging the possible encoding

Decode and check.

Write mode operation file

Step Two, write the file

python-files and built-in functions open (), file ()

Related Article

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.