codescracker


python

Python File I/O



« Previous Tutorial Next Tutorial »


As you know that the output is produced by using the function print() in python. Here is an example uses print() function:

# Python File IO - Example Program

print("This is python file io tutorial.");

Here is the sample output produced by the above python program:

python file io

Open a File in Python

You can use open() function to open a file in python. Here is an example.

c = open("myfile.txt")

You can specify the mode when opening the file. Here, the following table lists the modes available in python:

Mode Meaning
r Opens a file for reading only
r+ Opens a file for reading and writing both
rb Opens a file for reading only in binary format
rb+ Opens a file for reading and writing both, in binary format
w Opens a file for writing only. This mode creates a new file if not exist
w+ Opens a file for reading and writing both.
wb Opens a file for writing only in binary format
wb+ Opens a file for reading and writing both, in binary format
a Opens a file for appending. This mode also creates a new file if not exist
a+ Opens a file for appending and reading both
ab Opens a file for appending in binary format
ab+ Opens a file for appending and reading both, in binary format

Let's take a look at this example in python:

c = open("myfile.txt", 'w')		# open in writing mode
c = open("myfile.txt", 'r')		# open in reading mode
c = open("myfile.txt", 'a')		# open in appending mode

Close a File in Python

Use close() function to close an opened file in your python program like this:

c = open("myfile.txt", "r")
c.close()

Write to File in Python

Here is an example shows how to write to file in python. This program, creates a file name myfile.txt and then writes some line of text inside it.

# Python File IO - Python File Handling - Example Program

c = open("myfile.txt", 'w')

print("File created, successfully..!!");
print("writing some content inside the file....\n");

c.write("this is python file io tutorial\n");
c.write("this is python file io example\n");
c.write("this is python file handling tutorial\n");
c.write("this is python file handling example\n");

c.close()

print("File successfully closed..!!"); 

After running the above python program, there is a file named myfile.txt is created inside the current directory, containing these four line of text:

Here is the snapshot, shows the created file named myfile.txt inside the current directory:

python file handling

Now, if you try to open the file shown above myfile.txt. It looks like:

python file handling programs

Read from File in Python

Use read() function to read the content present inside the file. Here is an example to read content from a file in python:

# Python File IO - Python File Handling - Example Program

c = open("myfile.txt", 'r');

print("File, myfile.txt, opened successfully..!!");
print("The file contains :\n");
print(c.read());

c.close();

print("The file closed successfully..!!");

The above python program, reads the content present inside the file myfile.txt (created earlier). Here is the sample output produced by the above python program:

python read from write to file

More Examples

Here are list of some more examples on file handling in python:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test