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

Python read file

編輯:Python

First create a new file ty.txt, Write the following in it :

Line 1
Line 2
Line 3

Create a new one under the same directory Python file , open .

Read the file

Use open Method to open a file :

open(r".\ty.txt","r")

open The first argument to the function is the file name , The second is the opening method . Because I want to read the file , So choose "r" Pattern .

There are still some parameters that we haven't filled in : This will be answered in detail later .

After opening, you need to read the content .

1)read()

a=open(r".\ty.txt","r").read()
print(a)

Output :

Line 1
Line 2
Line 3

read Function provides a int Type parameter , Indicates the number of read characters ( Default = all ). example :

a=open(r".\ty.txt","r").read(1)
print(a)

Output :

L

2)readline()

a=open(r".\ty.txt","r")

Create a file object .

t1=a.readline()
t2=a.readline()
t3=a.readline()
print(t1)
print(t2)
print(t3)

result :

Line 1
Line 2
Line 3

There are more line breaks because readline A newline character is returned at the end of the line , add print The line break that comes with it , One more. .

readline There is a function with read Parameters with the same function . See above .

notes : If it were readline once , That is, it exceeds the total number of rows , The return value is null .

3)readlines()

a=open(r".\ty.txt","r")
t1=a.readlines()
print(t1)

Return value :

['Line 1\n', 'Line 2\n', 'Line 3']

Returns... As a string list .

There is a parameter :hint.

hint Usage of parameters

About python in readlines() Function hint Use of parameters _ Running caterpillar blog -CSDN Blog _python readlines Parameters Use readlines(): Used to read multiple lines of data from a file or stream at once , The returned data is stored in a list . The basic format for reading file contents is : File object .readlines(hint) for example :fname=open("《 Chengdu 》 The lyrics .txt","rt",encoding="utf-8") Then there are :fname.readlines(hint) among hint The value of is troublesome , Look up a lot of information on the Internet , A lot of people went into the pit , Finally, I summed it up by myself :...https://blog.csdn.net/cicisensy/article/details/105755245【 I won't , See the above great God blog】

attach : Different forms of reading

1)

text=open("...","r").read()

2)

a=open("...","r")
text=a.read()

3)

with open("...","r") as a:
text=a.read()

4)

a=open("...","r")
text=""""""
for i in a.readlines():
text=text+i

-------------------------------------- End -------------------------------------

Here is Unconquerable&Llxy, Personal home page =

Unconquerable&Llxy The blog of _CSDN Blog -Python From negative infinity to ~,Vpython-3D,our project1 Domain Blogger Unconquerable&Llxy Good at Python From negative infinity to ~,Vpython-3D,our project1, And so on ,Unconquerable&Llxy Focus on python field .https://blog.csdn.net/html_finder Welcome to visit :-)


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