The input Function
The input() function is used to get data from the user in Python Command Line programs.
<return> = input( "<prompt>")
Where prompt is a string that will be display for the user to view which should provide some kind of indication of the usage and type of data you are expecting and return is the returned value from the function.
>>> i = "Hello Intellipaat" >>> x = input("enter a value:") enter a value: i >>> print i
raw_input() function is used to take input from the user. It takes the input from the Standard input in the form of a string and reads the data from a line at once.
i=raw_input("Enter value "); print i
raw_input() does not deal with numbers, at least not directly.
x = raw_input("Enter your marks: ") Enter your marks: 20 >>> print x
Then it shows error because numbers are not handled by raw_input() directly. To use numbers conversion is used.
x = int ( raw_input("Enter your marks: ") ) Enter your marks: 20 print x
Then it displays 20.
Print statement is used to get the output.
x=20 print x
File is an external storage on hard disk from where data can be stored and retrieved. Python supports reading data form files and writing data to the files.
Operations on Files:
- Opening a file – To open file built in function open() is used. It returns an object of File which is used with other functions.
obj=open(file_name , access_mode , buffer)
file_name specify that file which you want to open.
access_mode specify the access_mode in which the file has to be open, i.e., read, write, append, etc.
buffer – It represents that buffering is performed or not, if buffer value is 0 then no buffering is performed and when buffer value is 1 then line buffering is performed while accessing the file.
|r||Opens a file for reading|
|rb||Opens a file for reading only in binary format.|
|w||Opens a file for writing only. Overwrites the file if the file exists.|
|wb||Opens a file for writing only in binary format.|
|a||Opens a file for appending. It does not overwrite the file just add the data in the file and if file is not created then it creates new file|
|ab||Opens a file for appending in binary format.|
2. Closing a File: It is used to close a file. For this purpose close() function is used.
3.Writing to a File: write() method is used to write a string into the file.
File_object.write(string str) i=open("intellipaat.txt","w") i.write("Hello Intellipaat") i.close()
4. Reading from a File: read() method is used to read data from the File.
j=open("intellipaat.txt","r") k=j.read() print k j.close()
Methods in File Handling
There are different methods are used which are as follows:
- rename() : This is used to rename a file.
os.rename(existing file_name, new file_name)
import os os.rename("abc.txt","xyz.txt")
- remove(): This method is used to delete a file.
import os os.remove("abc.txt")
- chdir(): This method is used to change the current directory.
import os os.chdir("new directory path")
- mkdir() : This method is used to create a directory.
import os os.mkdir("abc")
- rmdir() : This method is used to remove the directory.
import os os.rmdir("abc") // where abc is a directory name which you want to remove
- getcwd()– This method is used to show the current working directory.
import os print os.getcwd()
This blog will help you get a better understanding of Automate Your Coding with Python!