【Python】文件对象的声明及基本操作

2018-08-24  本文已影响64人  Natsuka
path1 = 'C:\\Users\\Administrator\\Desktop\lianxi'
path2 = r'C:\Users\Administrator\Desktop\lianxi'
path3 = 'test.text'

新建一个txt文件,用open读取。

path = r'C:\Users\Administrator\Desktop\lianxi\test.txt'
f = open(path,'r')
print(type(f)) # <class '_io.TextIOWrapper'>
print(f) #<_io.TextIOWrapper name='C:\\Users\\Administrator\\Desktop\\lianxi\\test.txt' mode='r' encoding='cp936'>
print(f.read())
# hello word!
# Let's learn Python together.
f.seek(0) # 0
f.close()
上一篇 下一篇

猜你喜欢

热点阅读