---- Advanced file operations python

# File operations

#turn on

#open (file address, operating form):

w: writing r: reading b: Binary a: Append

Case Code:

#/这样的斜杠就一个,\\这样的斜杠要两个
fh = open("E:/Pythondemo/Python-test/PythonLX/demo1.txt","r")
# 读取文本的整个内容
data = fh.read()
print(data)
# 读取一行
data1 = fh.readlines()
print(data1)

'''
x = 0
while True:
    line2=fh.readline()
    if(len(line)==0 and x>10):
        break
    print(lines)
    x+=1
fh.close()

'''

operation result:

Writing the file # (w / a +)

#w是非追加写入,如果你还往文件写入内容。结果就是新的内容会将旧的内容覆盖
#解决办法可以使用“a+”方式写入内容
data = "一起学python!"
fh2=open("E:/Pythondemo/Python-test/PythonLX/demo2.txt","w")
fh2.write(data)
#这样可以写进去了即文本已创建可是无内容,解决办法只有关闭了才能保存即可显示内容
fh2.close()

# Close the file

fh.close()

Published 98 original articles · won praise 34 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_42133768/article/details/96425487