Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Best Way to learn python in simple method

Rating
-
Sold
-
Pages
16
Uploaded on
02-05-2024
Written in
2023/2024

Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming

Show more Read less
Institution
Course

Content preview

UNIT IV
FILES ,MODULES, PACKAGES
File is a named location on disk to store related information. It is used to
permanently store data in a non-volatile memory (e.g. hard disk).
Since, random access memory (RAM) is volatile which loses its data when
computer is turned off, we use files for future use of the data.
When we want to read from or write to a file we need to open it first. When
we are done, it needs to be closed, so that resources that are tied with the file are
freed. Hence, in Python, a file operation takes place in the following order.
1. Open a file
2. Read or write (perform operation)
3. Close the file
Opening a file
Python has a built-in function open() to open a file. This function returns a file
object, also called a handle, as it is used to read or modify the file accordingly.
>>> f = open("test.txt") # open file in current directory
>>> f = open("C:/Python33/README.txt") # specifying full path
We can specify the mode while opening a file. In mode, we specify whether we
want to read 'r', write 'w' or append 'a' to the file. We also specify if we want to
open the file in text mode or binary mode.
The default is reading in text mode. In this mode, we get strings when reading from
the file. On the other hand, binary mode returns bytes and this is the mode to be
used when dealing with non-text files like image or exe files.
PYTHON FILE MODES:
Mode : Description
'r' : Open a file for reading. (default)
'w' : Open a file for writing. Creates a new file if it does not exist or truncates the
file if it exists.
'x' : Open a file for exclusive creation. If the file already exists, the operation fails.

,'a' : Open for appending at the end of the file without truncating it. Creates a new
file if it does not exist.
't' : Open in text mode. (default)
'b' : Open in binary mode.
'+' : Open a file for updating (reading and w


f = open("test.txt") # equivalent to 'r' or 'rt'
f = open("test.txt",'w') # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode


Hence, when working with files in text mode, it is highly recommended to specify
the encoding type.
f = open("test.txt",mode = 'r',encoding = 'utf-8')
Closing a File
When we are done with operations to the file, we need to properly close it.
Closing a file will free up the resources that were tied with the file and is done
using the close() method.
Python has a garbage collector to clean up unreferenced objects but, we must not
rely on it to close the file.
f = open("test.txt",encoding = 'utf-8')
# perform file operations
f.close()
This method is not entirely safe. If an exception occurs when we are performing
some operation with the file, the code exits without closing the file. A safer way is
to use a try...finally block.
try:
f= open("test.txt",encoding = 'utf-8')
# perform file operations
finally:

, f.close()


This way, we are guaranteed that the file is properly closed even if an
exception is raised, causing program flow to stop.
The best way to do this is using the with statement. This ensures that the file is
closed when the block inside with is exited.
We don't need to explicitly call the close() method. It is done internally.
with open("test.txt",encoding = 'utf-8') as f:
# perform file operations
READING AND WRITING:
A text file is a sequence of characters stored on a permanent medium like a hard
drive, flash memory, or CD-ROM.
To write a file, you have to open it with mode 'w' as a second parameter:
>>> fout = open('output.txt', 'w')
>>> print fout
<open file 'output.txt', mode 'w' at 0xb7eb2410>
If the file already exists, opening it in write mode clears out the old data and starts
fresh, so be careful! If the file doesn’t exist, a new one is created.
The write method puts data into the file.
>>> line1 = "This here's the wattle,\n"
>>>fout.write(line1)
Again, the file object keeps track of where it is, so if you call write again, it adds
the new data to the end.
>>> line2 = "the emblem of our land.\n"
>>> fout.write(line2)


When you are done writing, you have to close the file.
>>> fout.close()

Written for

Institution
Course

Document information

Uploaded on
May 2, 2024
Number of pages
16
Written in
2023/2024
Type
SUMMARY

Subjects

$9.09
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
aakashgurunathan

Get to know the seller

Seller avatar
aakashgurunathan Anna university
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions