程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics - files and exceptions

編輯:Python

Catalog

1. Reading data from a file

2. write file

3. abnormal

4. Store the data


1. Reading data from a file

1.1 Read entire file , Use with open

with open('C:\\Users\\Desktop\\ constant temperature 、 Heating and cooling .txt') as file_object:
contents = file_object.read()
print(contents.rstrip()) //rstrip() Delete ( Stripping ) Whitespace at the end of the string 

1.2 File path

similar :C:\\Users\\Desktop\\ constant temperature 、 Heating and cooling .txt

1.3 Read line by line

file_path = 'C:\\Users\\Desktop\\ constant temperature 、 Heating and cooling .txt'
with open(file_path) as file_object:
for line in file_object:
print(line.rstrip())

1.4 Use the contents of the file

Be careful : When reading a text file ,Python Read all the text in it as a string . If you read numbers , And use it as a number , You have to use functions int() Convert it to an integer , Or use functions float() Convert it to a floating point number .

2. write file

2.1 Write an empty file , Use write

filename = 'C:\\Users\\Desktop\\pi_million.txt'
with open(filename,'w') as file_object:
file_object.write("I love programing")

In this example , call open() Two arguments are provided when . The first argument is also the name of the file to open ; The second argument ('w' ) tell Python, We're going to open this file in write mode . When opening a file , The read mode can be specified ('r' )、 Write mode ('w' )、 Additional mode ('a' ) Or modes that allow you to read and write files ('r+' ). If you omit the pattern arguments ,Python The file will be opened in the default read-only mode .

If the file you want to write doesn't exist , function open() It will be created automatically . However , To write ('w' ) Be careful when opening files in mode , Because if the specified file already exists ,Python The file will be cleared before returning the file object .

Be careful :Python Only strings can be written to text files . To store numerical data in a text file , You have to use the function first str() Convert it to string format .

2.2 Write multiple rows

Is in the 2.1 Add the desired number of third lines of code on the basis of . Be careful , You need to add a newline character to each line \n

2.3 Attach to file --- have access to 'a'

If you want to add content to a file , Instead of covering the original content , You can open files in attach mode . When you open a file in attach mode ,Python The file will not be returned until the object is emptied , And the lines you write to the file will be added to the end of the file . If the specified file does not exist ,Python Will create an empty file for you . Same as 2.1 The code is consistent , take open In the parameters of the ’w‘ Change to ’a‘ that will do

3. abnormal

3.1ZeroDivisionError abnormal

 3.2 Use try-except Code block

When you think something might be wrong , You can write a try-except Code block to handle possible exceptions .

try:
print(5/0)
except ZeroDivisionError:
print("You can't divide by zero")

3.3 Use exceptions to avoid crashes --Try、 except ZeroDivisionError、 else Code

3.4 Handle FileNotFoundError abnormal

keyword :

Try:

        except FileNotFoundError

Try to use open Get a nonexistent file , Will report a mistake :FileNotFoundError: [Errno 2] No such file or directory: 'alice.txt', So it will try Statement placed within open() Before the line of code

3.5pass sentence

Use pass Statements in Except xx Add after statement , here Python Problems will not stop , Will continue to run

4. Store the data

4.1 Use json.dump() and json.load()

Use json.dump() Store the data 、json.load() Reading data

4.2 Save and read user generated data

Save user generated data , have access to input Let users input data , And then use 4.1 Storage data storage for

The learning content of this article refers to 《Python Programming : From introduction to practice 》


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved