python file operation example

#date = open('yesterday',encoding='utf-8').read() 
'''
f = open('yesterday','a+',encoding='utf-8') #File handle (file memory object): w write mode, r read mode, a append, r+ read and write
f = open('yesterday','r+',encoding='utf-8')
f = open('yesterday','w+',encoding ='utf-8')
f = open('yesterday','rb')#file handle, binary file network transmission, read binary file
f = open('yesterday','wb')#write as binary file#
data = f.read()
#print(data)
f.write('\nI love Beijing Tiananmen...\n')
f.write('The sun rises on Tiananmen')
data = f.read()
print(data )
f.close()
'''
''''
f = open('yesterday','r+',encoding='utf-8')
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
f.write('-------------------------------------------- ----')

print(f.readline())
f.close()
'''
f = open('yesterday','rb')
#f.write("hello binary\n".encode())
print(f.readline())
f.close()


'''
print(f.tell()) #Print the current position
print(f.readline())
print(f.readline())
print(f.readline ())
print(f.tell())
f.seek(10) #return place
print(f.readline())
print(f.encoding) #return encoding format
print (f.fileno()) #file Handle number
print(f.seekable())#Determine whether the cursor can be moved
print(f.readable())#Whether the file is readable
print(f.writable())#Determine whether the file can be written
'''

'''
f .write('hello 1\n')
print(f.buffer)
print(f.flush()) #flush to hard disk
f.seek(10)
f.truncate(20)#If you don't write it, you will clear it, and truncate
''' from the beginning

#for i in range(5):
# print(f.readline())
#print(f.readline() )
#for line in f.readline():
#print(f.readlines())
#low
'''
for index,line in enumerate(f.readlines()):#f.readlines() is suitable for small files
if index ==9:
print('-----------I am the 9th line------------')
continue
print(line.strip())
'''
#high bige read line by line, and only keep the same memory
''''
count = 0
for line in f:
if count ==9:
print('-------------I am the 9th line- ------------')
count += 1
continue
print(line.strip())
count +=1
'''


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325017497&siteId=291194637