任意の形式のテキストファイルの処理


#   --         
#    
# f = open('           ', '      ')
# f        ,        
# f.close()

#     :
# w. write  
# r read  
# a append      
import os 
%pwd
'C:\\study\\jupyter'
f = open('coop.txt', 'w')  #  w       ,      
f.write('coop' * 7)  #         
f.close()
with open('coop-1.txt', 'w') as f:
    f.write('coop' * 7)
with open('coop.txt') as f: #       r     
    data = f.read()#       ,       
    print(data)
coopcoopcoopcoopcoopcoopcoop
#                 ,  try except
with open('coop.txt', 'a') as f:
    f.write('coop1
') f.write('coop2
') f.write('
111') f.write('
222')
with open('coop.txt') as f:  
    print(f.readline())  #      
    print(f.readline())
    print(f.readline())
    print(f.readline())
coopcoopcoopcoopcoopcoopcoopcoop

coop

coop1

coop2
with open('coop.txt') as f:  #          readlines
    print(f.readlines())   #     
['coopcoopcoopcoopcoopcoopcoopcoop
', 'coop
', 'coop1
', 'coop2
', '
', '111
', '222']
with open('coop.txt') as f:  
    print(f.tell())  # tell()           (    )
    print(f.readline())  #      
    print(f.tell())
    print(f.readline())
    print(f.tell())
    print(f.seek(0))  # seek(0)        0   
    print(f.readline())
    print(f.readline())
    f.seek(5)

    print(f.readline())
    print(f.tell())
0
coopcoopcoopcoopcoopcoopcoopcoop

34
coop

40
0
coopcoopcoopcoopcoopcoopcoopcoop

coop

oopcoopcoopcoopcoopcoopcoop

34
f = open('coop.txt', 'a')
f.write('append
') # print(f.readlines())
7
with open('coop.txt',) as f:
    data = f.read()
    print(data)
coopcoopcoopcoopcoopcoopcoopcoop
coop
coop1
coop2

111
222appendappendappendappendappendappendappend
append
append
append
append
append
##############
#           
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '*.txt'):
        print(f)
    elif fnmatch.fnmatch(f, '*.pdf)'):
        print('find pdf', f)
coop-1.txt
coop.txt
#           
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '?+.txt'):  #   ?,    
        print(f)
    elif fnmatch.fnmatch(f, '?.pdf)'):
        print('find pdf', f)
#################
import fnmatch
for f in os.listdir('.'):
    if fnmatch.fnmatch(f, '\w+.txt'):  #   ?,    
        print(f)
    elif fnmatch.fnmatch(f, '?.pdf)'):
        print('find pdf', f)
#              
import glob
for f in glob.glob('[0-9].txt'):
    print(f)
0.txt
1.txt
import glob
for f in glob.glob('[0-9]+.txt'):  #     + ,       
    print(f)
############################
#     picle ,   ,   
#      ,    pkl
#   python     
name_list = ['coop', 'fang', 'beijing']
data = {'name':name_list, 'age':(2,3,4)}
import pickle
with open('data.pkl', 'wb') as f: #   wb,       
    pickle.dump(data, f)
with open('data.pkl', 'rb') as f:
    data = pickle.load(f)
    print(data)
{'name': ['coop', 'fang', 'beijing'], 'age': (2, 3, 4)}
############################
#     ,    ,         
import io
output = io.StringIO()
output.write('the first code
') print('ddd', file=output) # contents = output.getvalue() print(contents) # , output.close() #
the first code
ddd
#              python    pickle        
import shelve
with shelve.open('coop.she') as so:
    so['coop'] = 'fang'  #       
with shelve.open('coop.she') as so:
    print(so['coop'])
fang