0% found this document useful (0 votes)
31 views13 pages

Python InputOutput

The document discusses Python input/output (I/O) operations including reading input from the user, reading and writing to files, and different file modes. It explains that the input() function evaluates user input while raw_input() returns a string. It demonstrates opening, reading, writing, and closing files, and describes various file modes like read ("r"), write ("w"), and append ("a") that control file access.

Uploaded by

jerixxho tv
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
31 views13 pages

Python InputOutput

The document discusses Python input/output (I/O) operations including reading input from the user, reading and writing to files, and different file modes. It explains that the input() function evaluates user input while raw_input() returns a string. It demonstrates opening, reading, writing, and closing files, and describes various file modes like read ("r"), write ("w"), and append ("a") that control file access.

Uploaded by

jerixxho tv
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

Python I/O

Reading input
• raw_input()- This function reads only one line from the standard input
and returns it as a String.
• input()- The input() function first takes the input from the user and
then evaluates the expression, which means python automatically
identifies whether we entered a string or a number or list.
Reading input
• Structure
variable=input(“String”)

name=input(“Enter your name:”)


print(“Hello there, ”+name
Reading input
Num1=input(“Enter number”)
Num2=input(“Enter number”)
Print(Num1+Num2)

Num1=int(input(“Enter number”))
Num2=int(input(“Enter number”))
Print(Num1+Num2)
Files in Python
Syntax:

file_object = open(filename)

E.g.
Harry = open(“test.txt”)
RoqueSaBoracay= open(“C:/users/Python/test.txt”)
Reading the File
Itaas_Ang_Presyo_Ng_Palay = open(“Palay.txt”,”r”)
Print(Itaas_Ang_Presyo_Ng_Palay.read())

Itaas_Ang_Presyo_Ng_Palay = open(“Palay.txt”,”r”)
Print(Itaas_Ang_Presyo_Ng_Palay.readline())
Writing in File
Itaas_Ang_Presyo_Ng_Palay = open(“Palay.txt”,”w”)
Itaas_Ang_Presyo_Ng_Palay.write(“PatalsikinSiCynthiaVillar \n”)
Itaas_Ang_Presyo_Ng_Palay.write(“Magsaka Si Cynthia Challenge Now
Na!!”)
Closing File
Itaas_Ang_Presyo_Ng_Palay = open(“Palay.txt”,”w”)
Itaas_Ang_Presyo_Ng_Palay.write(“PatalsikinSiCynthiaVillar \n”)
Itaas_Ang_Presyo_Ng_Palay.write(“Magsaka Si Cynthia Challenge Now
Na!!”)
Itaas_Ang_Presyo_Ng_Palay.close()
Create File
nf=open(“listahan_ng_Utang_Ng_Pinas.txt”, “w”)
nf.close()
Deleting File
Import os
Os.remove(“listahan_ng_Utang_Ng_Pinas.txt”)
Modes
r
Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

rb
Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default
mode.

r+
Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

rb+
Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.

w
Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

wb
Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new
file for writing.

w+
Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a
new file for reading and writing.
Modes
wb+
Opens a file for both writing and reading in binary format. Overwrites the existing file if the file
exists. If the file does not exist, creates a new file for reading and writing.
a
Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the
file is in the append mode. If the file does not exist, it creates a new file for writing.
ab
Opens a file for appending in binary format. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for
writing
a+
Opens a file for both appending and reading. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it creates a new file for
reading and writing.
ab+
Opens a file for both appending and reading in binary format. The file pointer is at the end of
the file if the file exists. The file opens in the append mode. If the file does not exist, it creates
a new file for reading and writing.
File object and attributes
file.closed
Returns true if file is closed, false otherwise.
file.mode
Returns access mode with which file was opened.
file.name
Returns name of the file.
file.softspace
Returns false if space explicitly required with print, true otherwise.

You might also like