How to read files in python

藏色散人
Release: 2019-10-02 16:43:27
original
8599 people have browsed it

How to read files in python

How to read files in python?

First, create a txt document on the desktop and enter the following content:

你好。
Hello.
abcdefg
啊不错的风格
Copy after login

How to read files in python

Recommendation: "Python Tutorial

View the properties of the file and get the absolute path of the file:

D:\HintSoft\Hint-W7\Desktop
Copy after login

How to read files in python

##The file name is——New text document.txt,

Then, the absolute path plus the file name is the absolute file name:

D:\HintSoft\Hint-W7\Desktop\新建文本文档.txt
Copy after login

Open this file with python and name it f.

f = open(r"D:\HintSoft\Hint-W7\Desktop\新建文本文档.txt",'r')
Copy after login

How to read files in python

There is no output above, because after opening the file, the content has not been read:

f = open(r"D:\HintSoft\Hint-W7\Desktop\新建文本文档.txt",'r')
s=f.read()
print(s)
Copy after login

In this way, python returns all the information in the file .

How to read files in python

What if we only want to read the first 6 characters?

f = open(r"D:\HintSoft\Hint-W7\Desktop\新建文本文档.txt",'r')
s=f.read(6)
print(s)
Copy after login
In this way, only the first 6 characters will be returned.

How to read files in python

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

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!