First Python Programs : Text file « File « Python Tutorial

Home
Python Tutorial
1.Introduction
2.Data Type
3.Statement
4.Operator
5.String
6.Tuple
7.List
8.Dictionary
9.Collections
10.Function
11.Class
12.File
13.Buildin Function
14.Buildin Module
15.Database
16.Regular Expressions
17.Thread
18.Tkinker
19.wxPython
20.XML
21.Network
22.CGI Web
23.Windows
Python Tutorial » File » Text file 
12.3.1.First Python Programs
#!/usr/bin/env python

import os
ls = os.linesep

# get 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"

while True:
    entry = raw_input('> ')
    if entry == '.':
        break
    else:
        all.append(entry)

fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, lsfor x in all])
fobj.close()
print 'DONE!'
12.3.Text file
12.3.1.First Python Programs
12.3.2.File Read and Display
12.3.3.Demonstrates reading from a text file
12.3.4.Reads a plain text file
12.3.5.Writing to a text file
12.3.6.Open a file and save text to it
12.3.7.Storing and parsing Python objects in files
12.3.8.print to a file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.