User Input
The input Function
The input() function is used to get data from the user in Python Command Line programs.
Syntax

<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.
e.g.

>>> i = "Hello Intellipaat"
>>> x = input("enter a value:")
enter a value: i
>>> print i

Output
Hello Intellipaat
 
raw_input() function
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.
Syntax

raw_input(?statement?)

e.g.

i=raw_input("Enter value ");
print i

Output
Enter value
Hello intellipaat
 
raw_input() does not deal with numbers, at least not directly.
e.g.

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.
e.g.

x = int ( raw_input("Enter your marks: ") )
Enter your marks: 20
print x

Then it displays 20.
 
User Output
Print statement is used to get the output.
Syntax

print expression

e.g.

x=20
print x

Output
20
 
File Handling
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.

Syntax:

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.
 
Access Modes

ModesDescription
rOpens a file for reading
rbOpens a file for reading only in binary format.
wOpens a file for writing only. Overwrites the file if the file exists.
wbOpens a file for writing only in binary format.
aOpens 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
abOpens 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.
Syntax:

File_object.close()

 
3.Writing to a File: write() method is used to write a string into the file.
Syntax:

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.
Syntax:

File_object.read(data)

e.g.

j=open("intellipaat.txt","r")
k=j.read()
print k
j.close()

Output 
Hello Intellipaat
 
Methods in File Handling 
There are different methods are used which are as follows:

  • rename() : This is used to rename a file.

Syntax:

os.rename(existing file_name, new file_name)

eg:

import os
os.rename("abc.txt","xyz.txt")

 

  • remove(): This method is used to delete a file.

Syntax:

os.remove(file_name)

e.g.

import os
os.remove("abc.txt")

 

  • chdir(): This method is used to change the current directory.

Syntax:

os.chdir("new directory")

e.g.   

import os
os.chdir("new directory path")

 

  • mkdir() : This method is used to create a directory.

Syntax:

os.mkdir("new directory")

e.g.

import os
os.mkdir("abc")

 

  • rmdir() : This method is used to remove the directory.

Syntax

os.rmdir("directory name")

e.g.

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.

Syntax

os.getcwd()

e.g.

import os
print os.getcwd()

This blog will help you get a better understanding of Automate Your Coding with Python!

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
4 × 5 =