Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
19 views
File Handling 1
Uploaded by
PRITHVI STALIN BALU SANKAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save File Handling 1 For Later
Download
Save
Save File Handling 1 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
19 views
File Handling 1
Uploaded by
PRITHVI STALIN BALU SANKAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save File Handling 1 For Later
Carousel Previous
Carousel Next
Save
Save File Handling 1 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 6
Search
Fullscreen
PARTE: TEXT FILES IN THIS CHAPTER Introduction Vite J callection of data or information created Wn a secondary: stony Mevice std ay Yr disk, CD, pon drive, ete. Then are stilferent types af Hikes yor woul bace come acne slocument tile files, auulios tile Ailes, database hles ete Bache tile bs can in a specitic forma a particular same called | i with their format straw belo Doctunent tiles \ Video tiles Audio tiles Database files Archive files Fxecutable tiles Files in Python We can also eres the files are { { Program Fes Senne ot the tile type aby CENT aloe, galt, a In pag pit bmp, ete mpl, yp. anky, avd, ete map, way anuth, acode, trim, saptite, ete Zip, tar, dos, ete exe, alll, elves, ete je files in Python so that they can be used as and when required, tn Pythet sified into the following, categories Pytion Flos} | Data Fileschap Fis ansog program files When the user creates a program (code) in Python language, he she stores it in the secondary atorage device giving a specific name called as extension. The program file contains an pata files ou might have come across some situations where you nod fo handle a large volume of data (raw facts). Such data is further converted pa meaningful information after processing The input data is stored temporarily in the memory (RAM) betote the processing should take place, But as soon as processing is over the input data gets erased from the memory One the data gets erased, it will not be chained in future it required for re processing, From the above discussion, it must be clear to v ently (in secondary fate A fle that contains d. our discussion will b ‘our mind that the data need to be stored Mory) so that it can be used as and when require in the lata in the secondary storage is called In this chapter, ainly based on the dat Some terms related to the ata files MIA Daa Fes TB Result Passed with distinction Mentical fields in the same order. When your < the stake ame, it will become a of vour class in computer's memory with a specificEZ Text004 of Computor Science wih PYIRON Advantages of Data Files When a data file is created, it provides the following advan * The stored data resides permanently in the hand disk unless erased by the tser © The stored data can be shared with similar types of files * Its always possible to update the data as per our need Types of data files In Python Working with a file is said to be Python langage seals With thie deny types of files, They are (a) Text Files (b)_ Binary Files fo) CSV Files (a) Text Files Teat file ts a type of digital file which contains the data or information such as numbers, symbols and their combinations in ferms of strings. tevt tile is Iishatly human readable form that does not myquite ony spevtal formatting Hike document hie sy as alignment, bold underline, font style. ete. for its creation and storage. These files store the information in ASCH (American Standant Code tor Information and Interchange Unicode characters (default for programming platiorm) schemes of coding. In Python ¢ line in text file is terminated with a special character (\n), known as. Othenvise, all the lines in continuation will be treated as letters, Vo each a single tine (b) This is another concept of data file which stores data in terms ot binary digits (0's and 19) ‘The binary files are computer readable rather than human readable tevt files stored directly’ in the hard dish as they don't require any language translator. As a res these files are faster than the teat files for reading and writing data into the computers memory. These files at (c) CSV Files The word \ \ stands tor which is a simple text file format used to store data in a tabular form, such as a spreadsheet or database. ACs\ file stores the data (numbers and texts) in a tabular form as a plain text where, each now of the Ble is referred to as a Each record consists of one of mone |, separated by commas. In this chapter, we are going to focus on data files. The binary and CSV files will be discussed in the chapter ahead. Working with Text Files in Python The different tasks that take place during file operations are mentioned below 1. Opening a File 2. Closing a File 3. Writing Data into a File 4. Reading Data from a File Opening a text file in different modes ‘ To work with a file, the first and foremost requirement is to open a file. Unless the fit opened, you can’t perform any operation in it. When a file is opened, you need to neni the vid: in which file should work, The vs: states the specific operation to be carnal the file. The table shown below illustrates different modes along with their purposs which they are used while opening a file.chapter t= Fie anseg Bp Opens 9 text file in write Vt overwrites the file. if ex mesic only Rew file for writing + Otherwise, it create Opere @ text Ble in read: tt ts used to retrieve the reconiy from be inning of a file ror. This is mate on if it exists otherwise, results in an ¢ ault mode of a file. 2 Opensa text file in append The file pointer points the last record of the file, if the file exists In case, the file does not for wnting, He . it creates a new fi dded into the records a writes the existing fi SS not eust, it ere reading purposes if the file exists. If the a new file for writing and Opens a text file for both It allows reading as well as wnti g and whiting he records in a file. In order to retrieve the records, the file pointer is Placed initially at the first record of the file a Opens a text file for both The aPre read Pointer is placed at the end of the file for adding the records, if the exist, it cre: and reading, file exists. In case, the file does not ates a new file for reading and writing bb Python 2 file ts opened with the help of file or fle handle which is declared at the be; object "ginning of the Note 2 program Python has provided a built-in function * ‘ws’ mode erases the contents of a > open a file. This function retums the file object e-existing fo, # tho fie exists ands used to read, write and modify the file contents * rode 00s not eras the ely. I the work with one file is over, the same Nuns of the preexisting te Be object can be used to open another file. Syntax: Or Note. - By dotaul, a fie is name.txt ”) opened in read mode. Or £= open(name.txt ", w") The character 'f represents the file object which serves as a link to the file The “name.tnt” is the file name The character “w" represents the attribute of the file and states the mode in which the file is opened, Le., reading, writing or appending mode Path of a File : be Sometimes, it may happen that the file is not available in the current directory Harve then Se user may not be able to perform the task, although it is available in the computer aysien Ths task can be made easier by defining the path of a file along with function, Thus, ®e path of a file in a computer system is the route or the way to reach to a destination from the source to perform various tasks on it Syntax: tir > ED. CoH: SHonse ANNO Far examyve f= open(e\ Wython\\Name.tyt Modto (wit moda) Foe edyect Fig paty Fite Name Here, you can notice that the directory, sub-directory and 2 file name ane separated using two bck slashes (VY). Since, Note SE + the use of a single slash hasa spevial meaning (Le. escape THE US0 ot refx F malgs sequence) in its tse, so tho back slashes (\\) are used in {8 sting a raw string when, the path name of the file Indicates that thore is ng. meaning of using backslany However you can use character © as prefix ty the path character in the string nam ty use single slash (\) evplicitly as dinestories and file separator, the above syntay can be written as it vou wa t= open(r “C:\Python\Name.wvW") Components of a Path To understand various components of a path name, let us take an example: 2 tile Na Suppose, a eNt has been erated using the file path as: AH\Name.tut CAUsers\Cla The different components can be descrited as shown below C:\Users\ClassXI\Name.doey Raxt Foser Fie Name is creatad at the time of partitioning the ‘ . Note 2 nat the part of any other In MS DOS. the biden ae refered to as s. However. you can create new folders in the rot T (if neaied). * Sub-folder: Sub-folder is 2 folder created in the rot folder or in any other folder A subfolder may have another subfolder in it. In the example illustrated above, “Uses” is the sub-folder of the root folder whereas, “ClassXII” is the sub-sub-folder of the sub-folder “Users + Working folder: The working folder is said to be the current folder. It is a folder in which you are currently working, In the above example, “ClassXID” is the working folder + Parent folder: A folder whose level is one above the current folder or working folder In the example shown above, the “Users” is the parent folder. | Absolute and Relative Paths There are two ways to specify a file path. They are: It always begins with the root folder and shows the comp! {including folders and sub-folders) to reach to the specified file. The path tote shown in the above example is the absolute path. e path |Chapter 6-Filo Handing ED |. Relative Path: The relative path of a file is relative to the current folder. In other words, j you don't mention the root, the path will be relative, D show the eae Pa to a file, we use two special symbols such as single dot () and double do e single dot (.) and double dots (..) signify the presence of the srrent folder and pare fie in current folder and parent folder (ic., the folder one level above the current folder) spectively: » structure containing differe nance structure ning different folders, sub-folders and files is as illustrated below Tonnis doc t tems | Were, | = oe @ wereons rier B noesens ri With reference to the above tree structure, let us understand the relative path of different files in the table shown below: Tete Description Indoor \Tenis.doc “The file “Tenis,doc”is in working folder “Indoor” Indoor _\Name.txt ‘The file "Name.txt” is in parent folder "Games" |ndoor _\Outdoor\Cricketdoe In the parent folder "Games", the subfolder “Outdoor” contains the file "Cricket.doc’ ems |\Price.dat ‘The file "Price.dat" is in working folder “Items”. Items \Games\Indoor\Tennisdoc The file “Tennis doc” is in sub-folder “Indoor” that is a sub-folder of “Games” folder which is finally in the root folder. ems _AStock.xt The file “Stock.txt” is available in the parent folder (ée., root folder)
You might also like
PYTHON_FILES
PDF
No ratings yet
PYTHON_FILES
37 pages
Chapter 4 File Handlinf Final (New)
PDF
No ratings yet
Chapter 4 File Handlinf Final (New)
78 pages
CS - File Handling
PDF
No ratings yet
CS - File Handling
15 pages
Python Data File Handling XII CS 2022-23 As On 28-10-2022
PDF
No ratings yet
Python Data File Handling XII CS 2022-23 As On 28-10-2022
62 pages
PPS - NOTES - Unit-6 .
PDF
No ratings yet
PPS - NOTES - Unit-6 .
14 pages
Unit 5 - Files and Exceptions
PDF
No ratings yet
Unit 5 - Files and Exceptions
45 pages
File Handling3
PDF
No ratings yet
File Handling3
38 pages
File Handling 2
PDF
No ratings yet
File Handling 2
6 pages
Study Material File Handling 2024-25
PDF
No ratings yet
Study Material File Handling 2024-25
82 pages
Unit 3: Exceptions and Files
PDF
No ratings yet
Unit 3: Exceptions and Files
96 pages
file handling
PDF
No ratings yet
file handling
20 pages
File Handling
PDF
No ratings yet
File Handling
8 pages
F Open ("Test - TXT") F Open ("C:/Python33/README - TXT") # Specifying Full Path
PDF
No ratings yet
F Open ("Test - TXT") F Open ("C:/Python33/README - TXT") # Specifying Full Path
11 pages
Unit V
PDF
No ratings yet
Unit V
32 pages
Module8 - File Operations
PDF
No ratings yet
Module8 - File Operations
21 pages
3a.file Hadling Online_text
PDF
No ratings yet
3a.file Hadling Online_text
121 pages
File Handling
PDF
No ratings yet
File Handling
7 pages
Python Unit4 (Original)
PDF
No ratings yet
Python Unit4 (Original)
45 pages
unit V
PDF
No ratings yet
unit V
12 pages
Chapter 5 Text and Binary File Handling
PDF
100% (1)
Chapter 5 Text and Binary File Handling
36 pages
File Handling
PDF
No ratings yet
File Handling
46 pages
File Handling in Python
PDF
No ratings yet
File Handling in Python
65 pages
Files
PDF
No ratings yet
Files
17 pages
Introduction To Files
PDF
No ratings yet
Introduction To Files
17 pages
Chapter 5 FileHandlingFinal
PDF
No ratings yet
Chapter 5 FileHandlingFinal
56 pages
File Handling Notes
PDF
No ratings yet
File Handling Notes
28 pages
GE3151 Unit V
PDF
No ratings yet
GE3151 Unit V
20 pages
Chapter-4-Data-File-Handling Cs Sa
PDF
No ratings yet
Chapter-4-Data-File-Handling Cs Sa
19 pages
23CS101T PSPP - Unit 5
PDF
No ratings yet
23CS101T PSPP - Unit 5
32 pages
04 File Handling
PDF
No ratings yet
04 File Handling
40 pages
Problem Solving and Python Programming
PDF
No ratings yet
Problem Solving and Python Programming
28 pages
Notes
PDF
No ratings yet
Notes
64 pages
Python Ch_5 File Handling_XII
PDF
No ratings yet
Python Ch_5 File Handling_XII
95 pages
10._ File Handling in Python.docx
PDF
No ratings yet
10._ File Handling in Python.docx
26 pages
Python
PDF
No ratings yet
Python
10 pages
file_handling
PDF
No ratings yet
file_handling
28 pages
UNIT 4 Python
PDF
No ratings yet
UNIT 4 Python
28 pages
Lec 17. File Handling
PDF
No ratings yet
Lec 17. File Handling
29 pages
F 12 CH 04 TEXT FILE HANDLING 1
PDF
No ratings yet
F 12 CH 04 TEXT FILE HANDLING 1
111 pages
Lesson 5 File Handling Text Files
PDF
No ratings yet
Lesson 5 File Handling Text Files
35 pages
File Handling
PDF
No ratings yet
File Handling
260 pages
Lec 14. File Handling
PDF
No ratings yet
Lec 14. File Handling
29 pages
chapter-4-data-file-handlingeng
PDF
No ratings yet
chapter-4-data-file-handlingeng
19 pages
Os Chapter 6
PDF
No ratings yet
Os Chapter 6
31 pages
FILE HANDLING IN PYTHON NOTES Final
PDF
No ratings yet
FILE HANDLING IN PYTHON NOTES Final
15 pages
Hanling
PDF
No ratings yet
Hanling
7 pages
Presentation 1
PDF
No ratings yet
Presentation 1
44 pages
Cbse Class 12 Computer Science File Handling4
PDF
No ratings yet
Cbse Class 12 Computer Science File Handling4
39 pages
File Handling 1
PDF
No ratings yet
File Handling 1
14 pages
Class XII (As Per CBSE Board) : Computer Science
PDF
No ratings yet
Class XII (As Per CBSE Board) : Computer Science
38 pages
lecs102
PDF
No ratings yet
lecs102
21 pages
Text and Binary File-1
PDF
No ratings yet
Text and Binary File-1
10 pages
File Handling in Python
PDF
No ratings yet
File Handling in Python
20 pages
File Handling in Python
PDF
No ratings yet
File Handling in Python
65 pages
08 Slide Extended
PDF
No ratings yet
08 Slide Extended
44 pages
File Handlling Demo
PDF
No ratings yet
File Handlling Demo
54 pages
File Handling
PDF
No ratings yet
File Handling
43 pages
Introduction To File Handling
PDF
No ratings yet
Introduction To File Handling
4 pages