python读取文件问题

如果要读取本地文件,路径的格式非常重要

例如,windows下文件路径是这样:C:\Users\Root\Desktop\python\test.txt 

 #test.txt下内容有 hello world

                             ojbk   两行数据

如果要读取:C:/Users/Root/Desktop/python/test.txt                                                           

path = "C:/Users/Root/Desktop/python/test.txt"
with open(path,'r') as f:
    content = f.read()

print content

f.read()                                    #读取类似hello world

f.readline()                             #读入一行hello world

f.readlines()                           #读入全部数据并将每一行数据作为一个元素存放到数组中 ,['hello world\n', 'ojbk']

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/79677890