Python Input And Output

Python can be utilized to peruse and compose information likewise it backings perusing and composing information to Files.

  • "print" proclamation:
  • "print" proclamation is utilized to print the yield on the screen.
  • print proclamation is utilized to take string as data and spot that string to standard yield.

Whatever you need to show on yield put that expression inside the upset commas. The expression whose quality is to printed spot it without altered commas.

Data from Keyboard

Python offers two in-constructed capacities for taking info from client. They are:

  • data()
  • raw_input()
  • data() functioninput() capacity is utilized to take info from the client. Whatever expression is given by the client, it is assessed and result is returned back.
  • raw_input()raw_input() capacity is utilized to take information from the client. It takes the info from the Standard information as a string and peruses the information from a line on the double.

Modes of File:

There are different modes of file in which it can be opened. They are mentioned in the following table.

A File can be opened in two modes:

1 Text Mode.

2 Binary Mode.

Mode Description
R It opens in Reading mode. It is default mode of File. Pointer is at beginning of the file.
rb It opens in Reading mode for binary format. It is the default mode. Pointer is at beginning of file.
r+ Opens file for reading and writing. Pointer is at beginning of file.
rb+ Opens file for reading and writing in binary format. Pointer is at beginning of file.
W Opens file in Writing mode. If file already exists, then overwrite the file else create a new file.
wb Opens file in Writing mode in binary format. If file already exists, then overwrite the file else create a new file.
w+ Opens file for reading and writing. If file already exists, then overwrite the file else create a new file.
wb+ Opens file for reading and writing in binary format. If file already exists, then overwrite the file else create a new file.
a Opens file in Appending mode. If file already exists, then append the data at the end of existing file, else create a new file.
ab Opens file in Appending mode in binary format. If file already exists, then append the data at the end of existing file, else create a new file.
a+ Opens file in reading and appending mode. If file already exists, then append the data at the end of existing file, else create a new file.
ab+ Opens file in reading and appending mode in binary format. If file already exists, then append the data at the end of existing file, else create a new file.

Methods:

There are many methods related to File Handling. They are given in the following table:

There is a module "os" defined in Python that provides various functions which are used to perform various operations on Files. To use these functions 'os' needs to be imported.

Method Description
rename() It is used to rename a file. It takes two arguments, existing_file_name and new_file_name.
remove() It is used to delete a file. It takes one argument. Pass the name of the file which is to be deleted as the argument of method.
mkdir() It is used to create a directory. A directory contains the files. It takes one argument which is the name of the directory.
chdir() It is used to change the current working directory. It takes one argument which is the name of the directory.
getcwd() It gives the current working directory.
rmdir() It is used to delete a directory. It takes one argument which is the name of the directory.
tell() It is used to get the exact position in the file.