Python Experiment No: 7 Study and Implementation of File and directory

Aim:- Perform Cut, Copy, Paste Operations on File.
Theory:-
Input:-
 
Python has two functions designed for accepting data directly from the user:
1) Input()
2) Raw_input()

raw_input()
raw_input() asks the user for a string of data (ended with a newline), and simply returns the string. It can
also take an argument, which is displayed as a prompt before the user enters the data. E.g.
 
print raw_input('What is your name? ')
prints out
What is your name? <user input data here>

 
Example: in order to assign the user's name, i.e. string data, to a variable "x" you would type

x = raw_input('What is your name?')
Once the user inputs his name, e.g. Simon, you can call it as x
print ('Your name is ' + x)
prints out
Your name is Simon
 
input()input() uses raw_input to read a string of data, and then attempts to evaluate it as if it were a Python program, and then returns the value that results. So entering

[1,2,3]

would return a list containing those numbers, just as if it were assigned directly in the Python script. More complicated expressions are possible. For example, if a script says:

x = input('What are the first 10 perfect squares? ')
it is possible for a user to input:
map(lambda x: x*x, range(10))
 
which yields the correct answer in list form. Note that no inputted statement can span more than one line.input() should not be used for anything but the most trivial program. Turning the strings returned
from raw_input() into python types using an idiom such as:

x = None
while not x:
try:
x = int(raw_input())
except ValueError:
print 'Invalid Number'

is preferable, as input() uses eval() to turn a literal into a python type. This will allow a malicious person to run arbitrary code from inside your program trivially.
File Input:-
File Objects

Python includes a built-in file type. Files can be opened by using the file type's constructor:

f = file('test.txt', 'r')

This means f is open for reading. The first argument is the filename and the second parameter is the mode, which can be 'r', 'w', or 'rw', among some others.
The most common way to read from a file is simply to iterate over the lines of the file:

f = open('test.txt', 'r')
for line in f:
print line[0]
f.close()

This will print the first character of each line. Note that a newline is attached to the end of each line read this way.
Because files are automatically closed when the file object goes out of scope, there is no real need to close them explicitly. So, the loop in the previous code can also be written as:

for line in open('test.txt', 'r'):
print line[0]

It is also possible to read limited numbers of characters at a time, like so:

c = f.read(1)
while len(c) > 0:
if len(c.strip()) > 0: print c,
c = f.read(1)

This will read the characters from f one at a time, and then print them if they're not whitespace.


Q1) Give difference between input() and rawinput() method.
Q2) How to open the file and different file opening mode available in python.
Q3) Write syntax to copy one file to another file.

Comments