Python:with open() as f:


1 with open() as f
1.1一般的な読み書き操作
with open(r'filename.txt') as f:
   data_user=pd.read_csv(f)  #      

with open('data.txt', 'w') as f:
   f.write('hello world')  #      
   

1.2関連パラメータ
r:	         。              。  **    **。

rb:                 。             。      。

r+:           。             。

rb+:                。             。

w:	           。             。        ,     。

wb:	                 。             。        ,     。

w+:	          。             。        ,     。

wb+:                。             。        ,     。

a:	          。        ,             。    ,                。        ,         。

ab:	                。        ,             。    ,                。        ,         。

a+:	          。        ,             。           。        ,         。

ab+:                。        ,             。        ,         。


1.3 fileオブジェクトのプロパティ
file.read([size])               ,    size        

file.readlines([size])              ,size    

file.write(str)           

file.writelines(strings)             

file.close()       

file.closed	         ,   False

file.mode	Access            

file.encoding	        

file.name	   

file.newlines	          None,               ,              ,                    

file.softspace	 0         ,        ,1    。            ,       


2栗
import os


db_root_dir = '../../DAVIS-2016'
fname = 'train_seqs'
path = os.path.join(db_root_dir, fname + '.txt')
print(path)

with open(path) as f:
    seq = f.readlines()
    print(seq)
    print(len(seq))


# results

../../DAVIS-2016/train_seqs.txt

['bear
', 'bmx-bumps
', 'boat
', 'breakdance-flare
', 'bus
', 'car-turn
', 'dance-jump
', 'dog-agility
', 'drift-turn
', 'elephant
', 'flamingo
', 'hike
', 'hockey
', 'horsejump-low
', 'kite-walk
', 'lucia
', 'mallard-fly
', 'mallard-water
', 'motocross-bumps
', 'motorbike
', 'paragliding
', 'rhino
', 'rollerblade
', 'scooter-gray
', 'soccerball
', 'stroller
', 'surf
', 'swing
', 'tennis
', 'train
'] 30

参照先:
  • https://blog.csdn.net/msspark/article/details/86745391

  •