Pythonベース10:ファイル操作とIO操作

1821 ワード

'''
  :         (       )
   (redis mysql)
             
'''
my_list = []
my_list.append(1)
print(my_list)

'''
              
               (          )
'''

'''
       
'''

'''
r  (  ),        
w  (  )
a  (  )
rb  :           (  )
wb  :           (  )
ab  :           (  )
'''
file = open('1.txt','r') #      
#          
content = file.read()
print(content)
#    (   )
file.close()

'''
        ,             ,     
windows       gbk
mac   utf-8
'''
file = open('1.txt','w',encoding = 'utf-8') #      
#          
file.write('111')
file.write('222')
#    (   )
file.close()

#        
result = file.encoding
print(result)

file = open('1.txt', 'a', encoding='utf-8')
file.write('444')


'''
rb、wb、ab  ,     encoding
         
'''
file = open('1.txt','rb')
file_data = file.read()
content = file_data.decode('utf-8')
print(file_data,content)


'''
r+,w+,a+,    
rb+,wb+,ab+,       ,     ,     
'''





'''
StringIO,        
'''
import io
str_io = io.StringIO()

#        
str_io.write('hello')
str_io.write('world')

#        
content = str_io.getvalue()
print(content)

str_io.seek(0)
#          
print(str_io.tell())
result = str_io.read()
print(result) # ,             

'''
ByteIO,          
'''
from io import BytesIO
byte_io = BytesIO()
#   :               
byte_io.write('ha '.encode('utf-8'))

#     ,          
data = byte_io.getvalue()

#  ,            
print(data.decode('utf-8'))