python - What is the most pythonic way to open a file? -
i'm trying clean code little bit, , have trouble figuring of these 2 ways considered pythonic one
import os  dir = os.path.dirname(__file__) str1 = 'filename.txt' f = open(os.path.join(dir,str1),'r')   although second seems cleanest one, find declaration of fullpath bit much, since used once.
import os  dir = os.path.dirname(__file__) str1 = 'filename.txt' fullpath = os.path.join(dir,str1) f = open(fullpath,'r')   in general, better thing avoid calling functions inside of call, if adds line of code ?
with open('file path', 'a') f:    data = f.read()    #do data   or
f = open(os.path.join(dir,str1),'r') f.close()      
Comments
Post a Comment