1 讀取txt檔案。跟c相比,python的檔案讀寫簡直是方便的可怕
首先是讀取檔案
首先獲得檔名稱,然後通過 open函式開啟檔案,通過for迴圈逐行讀出檔案內容
#2 寫入檔案並儲存,同理,新建乙個檔案,也是通過open函式。神奇。!python file by ninahao 10.30
'readfile.py--read and display text file'#
get filename
fname=raw_input('
enter file name:')
#attempt to open file for reading
try:
fobj=open(fname,'r'
)except
ioerror,e:
'open file--
',fname,'
--error!:',e
else
:
#display content to the screen
for eachline in
fobj:
eachline
fobj.close()
###write lines to file with proper line-ending!maketext.py
'maketextfile.py--create text file
'import
osls=os.linesep
#get filename
while
true:
fname=raw_input('
enter file name : ')
ifos.path.exists(fname):
"error: '%s' already exists
" %fname
else
:
break
#get file content(text) lines
all=
"\nenter lines('.' by itself to quit).\n"#
loop until user terminates input
while
true:
entry=raw_input('
>')
if entry=='.'
:
break;
fobj=open(fname,'w')
fobj.writelines(['%s%s' % (x,ls) for x in all])
fobj.close()
print 'done!'
java生成txt檔案,讀txt檔案
1.方法1 public static void main string args catch exception e system.out.println write end try filereader.close catch exception e system.out.println rea...
txt檔案讀操作
名稱 product.txt 檔案內容 1 f 開啟檔案product.txt 2 f.readlins 讀出檔案的所有行,每一行乙個字串,例如 iphone 9929 n 3 lines 由每一行變成的字串組成的列表 4 line lines的元素,即上面的字串。5 列印的結果是字串中的內容,即 ...
python 簡單的txt檔案讀寫
1 讀取txt檔案。跟c相比,python的檔案讀寫簡直是方便的可怕 首先是讀取檔案 首先獲得檔名稱,然後通過 open函式開啟檔案,通過for迴圈逐行讀出檔案內容 python file by ninahao 10.30 readfile.py read and display text file...