Return to Snippet

Revision: 6702
at June 9, 2008 09:49 by chombee


Updated Code
# The built-in function `open` opens a file and returns a file object.

# Read mode opens a file for reading only.
try:
    f = open("file.txt", "r")
    try:
        # Read the entire contents of a file at once.
        string = f.read() 
        # OR read one line at a time.
        line = f.readline()
        # OR read all the lines into a list.
        lines = f.readlines()
    finally:
        f.close()
except IOError:
    pass


# Write mode creates a new file or overwrites the existing content of the file. 
# Write mode will _always_ destroy the existing contents of a file.
try:
    # This will create a new file or **overwrite an existing file**.
    f = open("file.txt", "w")
    try:
        f.write('blah') # Write a string to a file
        f.writelines(lines) # Write a sequence of strings to a file
    finally:
        f.close()
except IOError:
    pass

# Append mode adds to the existing content, e.g. for keeping a log file. Append
# mode will _never_ harm the existing contents of a file.
try:
    # This tries to open an existing file but creates a new file if necessary.
    logfile = open("log.txt", "a")
    try:
        logfile.write('log log log')
    finally:
        logfile.close()
except IOError:
    pass

# There is also r+ (read and write) mode.

Revision: 6701
at June 9, 2008 09:48 by chombee


Initial Code
# The built-in function `open` opens a file and returns a file object.

# Read mode opens a file for reading only.
try:
    # This will create a new file or **overwrite an existing file**.
    f = open("file.txt", "r")
    try:
        # Read the entire contents of a file at once.
        string = f.read() 
        # OR read one line at a time.
        line = f.readline()
        # OR read all the lines into a list.
        lines = f.readlines()
    finally:
        f.close()
except IOError:
    pass


# Write mode creates a new file or overwrites the existing content of the file. 
# Write mode will _always_ destroy the existing contents of a file.
try:
    # This will create a new file or **overwrite an existing file**.
    f = open("file.txt", "w")
    try:
        f.write('blah') # Write a string to a file
        f.writelines(lines) # Write a sequence of strings to a file
    finally:
        f.close()
except IOError:
    pass

# Append mode adds to the existing content, e.g. for keeping a log file. Append
# mode will _never_ harm the existing contents of a file.
try:
    # This tries to open an existing file but creates a new file if necessary.
    logfile = open("log.txt", "a")
    try:
        logfile.write('log log log')
    finally:
        logfile.close()
except IOError:
    pass

# There is also r+ (read and write) mode.

Initial URL

                                

Initial Description

                                

Initial Title
Reading and writing text files in Python

Initial Tags

                                

Initial Language
Python