
In this tutorial, we will see File IO.
Reading Keyboard Input
Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are −
- raw_input
- input
The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline).
str = raw_input("Enter your input: ");
print "Received input is : ", str
This prompts you to enter any string and it would display same string on the screen. When I typed “Hello Python!”, its output is like this −
Enter your input: Hello Python Received input is : Hello Python
The input Function
The input([prompt]) function is equivalent to raw_input, except that it assumes the input is a valid Python expression and returns the evaluated result to you.
str = input("Enter your input: ");
print "Received input is : ", str
This would produce the following result against the entered input −
Enter your input: [x*5 for x in range(2,10,2)] Recieved input is : [10, 20, 30, 40]
Opening and Closing Files:
Python provides basic functions and methods necessary to manipulate files by default. You can do most of the file manipulation using a file object.
The open Function:
Before you can read or write a file, you have to open it using Python’s built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.
Syntax
file object = open(file_name [, access_mode][, buffering])
Here are parameter details −
- file_name − The file_name argument is a string value that contains the name of the file that you want to access.
- access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r).
- buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default.
file Object Attributes
Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object −
| Sr.No. | Attribute & Description |
|---|---|
| 1 | file.closed– Returns true if file is closed, false otherwise. |
| 2 | file.mode– Returns access mode with which file was opened. |
| 3 | file.name– Returns name of the file. |
| 4 | file.softspace-Returns false if space explicitly required with print, true otherwise. |
close() Method:
The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.
Syntax : fileObject.close();
write() Method
The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text.The write() method does not add a newline character (‘\n’) to the end of the string −
Syntax : fileObject.write(string);
Here, passed parameter is the content to be written into the opened file.
# Example
# Open a file
fo = open("data.txt", "wb")
fo.write( "Hello Students DAta is here!!\n");
# Close opend file
fo.close()
read() Method
The read() method reads a string from an open file. It is important to note that Python strings can have binary data. apart from text data.
Syntax : fileObject.read([count]);
Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading from the beginning of the file and if count is missing, then it tries to read as much as possible, maybe until the end of file.
rename() Method
The rename() method takes two arguments, the current filename and the new filename.
Syntax : os.rename(current_file_name, new_file_name)
remove() Method
You can use the remove() method to delete files by supplying the name of the file to be deleted as the argument.
Syntax : os.remove(file_name)


Leave a reply to Siddhant Cancel reply