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 ");
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
Then it displays 20.
Print statement is used to get the output.
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:
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.
||Opens a file for reading
||Opens a file for reading only in binary format.
||Opens a file for writing only. Overwrites the file if the file exists.
||Opens a file for writing only in binary format.
||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
||Opens a file for appending in binary format.
Become a Python Expert, Enroll in our Python training in Bangalore.
Closing a File:
It is used to close a file. For this purpose close() function is used.
Writing to a File:
write() method is used to write a string into the file.
Reading from a File:
read() method is used to read data from the File.
Intellipaat is providing the best platform for Python. Enroll in our Python Training and learn from Professional Industrial Experts.
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)
- remove(): This method is used to delete a file.
- chdir(): This method is used to change the current directory.
os.chdir("new directory path")
- mkdir() : This method is used to create a directory.
- rmdir() : This method is used to remove the directory.
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.
This blog will help you get a better understanding of Automate Your Coding with Python!