建立和讀出檔案p2.py和p3.py
#!/usr/bin/python
import os
ls=os.linesep
fname = raw_input('Enter filename: ')
while True:
if os.path.exists(fname):
print "Error '%s' already exists" % fname
else:
break
all=[]
print "\nEnter lines ('.') by itself to quit).\n"
entry=raw_input('>')
if entry=='.':
all.append(entry)
fobj=open(fname,'w')
執行python p2.py
讀出檔案
#!/usr/bin/env Python
'readTextFile.py -- read and display text file'
# get filename
# attempt to open file for reading
try:
fobj = open(fname, 'r')
except IOError, e:
print "*** file open error:", e
else:
# display contents to the screen
for eachLine in fobj:
print eachLine,
fobj.close()
執行python p3.py