A file in python usually categorized as either text or binary,a text file is often structured as a sequence of line and a line is a sequence of characters.the line is terminated by a EOF character.
The most common line end with ',' or the newline character.the backslash character indicates that the next character will be treated as a newline.
The open a file for writing use the buil-i open() function.open() return a file object,and is most commonly used with two arguments.
file_object = open(filename,mode)
The mode arguments is optional,the modes can be:
'r' - when the file will only be read.
'w' - for only writing.
'a' - Open the file for appending.
'r+' - open the file for both reading and writing.
f = open('testfile.txt','w');
In python create a text file first create a text file,you can name it anything you like following example.
f = open('testfile.txt','w');
The write method takes one parameter,which is the string to be written.
file = open("test.txt","w");
file.write("Welcome");
file.close();
In python read a file the following example of read file in python.
file = open('text.txt'.'r');
print file.read();
file.close();
When you are done with a file,call file.close() to close it and free up any system resources taken up by the open file.