SlideShare a Scribd company logo
1 of 11
File Handling
in Python
Introduction
File handling is the process of reading and
manipulating data from files.
In Python, file handling is a fundamental skill for
working with external data.
Python provides a rich set of built-in functions to
manage files.
Use cases: storing program data, user
configurations, logs, and exchanging data with
other systems.
We will delve into the essentials of file handling
using Python. We'll cover concepts such as opening
and closing files, reading data, writing content, and
different file modes. By the end, you'll have a solid
foundation for working with files effectively in your
Python projects.
Opening and
Closing Files
Use the open() function to open a file:
file_object = open("filename", "mode")
Common file modes: 'r' (read), 'w' (write),
'a' (append), 'x' (create)
Create new files if they don't exist (with 'w'
or 'x')
Always close files with file_object.close()
The open() function is the gateway to file
operations in Python. It creates a file
object that offers methods to interact with
the file. The mode determines your level of
access (read, write, etc). Closing files using
close() releases system resources and
prevents data corruption.
Reading Data from
Files
The read() method returns the entire content as a string.
The readline() method reads a single line at a time.
The readlines() method reads all lines into a list of strings.
Iterate over files using a for loop for line-by-line
processing.
Once a file is open in read mode, you have a variety of
options to extract data. Choose the method best suited to
your task—reading everything at once or working with
lines individually. Looping through files allows you to
process information systematically.
Writing Data to Files
Use the write() method to write a string to a file (mode 'w'
or 'a').
'w' mode overwrites existing content, 'a' mode appends to
the end.
Be careful about overwriting important files.
writelines() can write a list of strings to a file.
Files allow you to make your program's data persistent.
The write() method is your primary tool for saving data to
files. Manage how you modify existing files carefully, using
either 'w' for overwrites or 'a' to add new content.
File Modes
'r' (read): Open for reading (default). Raises an error if the
file doesn't exist.
'w' (write): Open for writing, overwrites existing content,
or creates a new file.
'x' (create): Creates a new file, fails if the file exists.
'a' (append): Open for writing, appends to the end, or
creates if it doesn't exist.
'b' (binary): Used for binary files (images, videos)
**'+' (update): ** For both reading and writing.
These file modes give you granular control over how you
interact with files. Choose the appropriate mode based
on whether you need to read existing data, create new
data, or update file contents.
Working with CSV Files
CSV (Comma-Separated Values) stores structured data in plain text.
Use Python's csv module for handling CSV files.
Import the module: import csv
Use csv.reader() to read CSV data into rows.
Use csv.writer() to write data into CSV format.
CSV is a common format for storing tabular data. Python's csv module
provides tools to easily read from and write data to CSV files, making it
a great choice for interchanging data between systems.
File Path
Management
Use the os module for file path operations.
os.path.exists() checks if a file or directory exists.
os.path.join() combines path components correctly.
os.path.split() separates the filename from the path.
Employ relative and absolute paths for file
references.
Organizing files and directories is crucial in larger
projects. The os module helps navigate your file
system—checking if files exist, constructing paths
properly, and understanding file locations within
your project structure.
Error Handling
Wrap file operations in try-except blocks.
Common file-related exceptions:
FileNotFoundError
IOError
PermissionError
Provide meaningful error messages to the user.
Ensure files are closed properly with a finally block
Files are external resources; things can go wrong (missing files, permissions). Error handling
with try-except makes your Python code robust and allows you to provide informative
error messages if file operations fail.
Conclusion Other Libraries: Explore libraries like pandas for
advanced data manipulation.
Binary Files: Learn about working with non-text
data (images, audio, etc.)
Context Managers: Use the with statement for
more elegant file handling (with open(...) as
file_object).
File Handling Best Practices: Emphasize the
importance of file handling in data-driven
Python projects.
These additional points hint at the broader world
of file handling. Python offers a rich ecosystem
of tools for data-related tasks, allowing you to
work with various file formats. Wrap-up the
presentation by reiterating the importance of file
handling as a fundamental building block for
many Python applications.
What are Python basics?
For Query Contact : 998874-1983

More Related Content

Similar to what are python basics.pptx.Join Python training in Chandigarh

Similar to what are python basics.pptx.Join Python training in Chandigarh (20)

Python-files
Python-filesPython-files
Python-files
 
File Handling
File HandlingFile Handling
File Handling
 
Python files / directories part15
Python files / directories  part15Python files / directories  part15
Python files / directories part15
 
File handling3.pdf
File handling3.pdfFile handling3.pdf
File handling3.pdf
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Switching & Multiplexing
Switching & MultiplexingSwitching & Multiplexing
Switching & Multiplexing
 
Module2-Files.pdf
Module2-Files.pdfModule2-Files.pdf
Module2-Files.pdf
 
pspp-rsk.pptx
pspp-rsk.pptxpspp-rsk.pptx
pspp-rsk.pptx
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Files in Python.pptx
Files in Python.pptxFiles in Python.pptx
Files in Python.pptx
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Data file handling
Data file handlingData file handling
Data file handling
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
File handling and Dictionaries in python
File handling and Dictionaries in pythonFile handling and Dictionaries in python
File handling and Dictionaries in python
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 

More from asmeerana605

Can we use SQL in java.pptx.Join SQL Training in Chandigarh
Can we use SQL in java.pptx.Join SQL Training in ChandigarhCan we use SQL in java.pptx.Join SQL Training in Chandigarh
Can we use SQL in java.pptx.Join SQL Training in Chandigarhasmeerana605
 
Can I learn PHP course in 3 months..pptx
Can I learn PHP course in 3 months..pptxCan I learn PHP course in 3 months..pptx
Can I learn PHP course in 3 months..pptxasmeerana605
 
Best Certificate Courses In Chandigarh.pptx-presentation
Best Certificate Courses In Chandigarh.pptx-presentationBest Certificate Courses In Chandigarh.pptx-presentation
Best Certificate Courses In Chandigarh.pptx-presentationasmeerana605
 
Android course in chandigarh.pptx-presentation
Android course in chandigarh.pptx-presentationAndroid course in chandigarh.pptx-presentation
Android course in chandigarh.pptx-presentationasmeerana605
 
Content Writing Course in Chandigarh.pptx
Content Writing Course in Chandigarh.pptxContent Writing Course in Chandigarh.pptx
Content Writing Course in Chandigarh.pptxasmeerana605
 
Graphic designing course in chandigarh.pptx
Graphic designing course in chandigarh.pptxGraphic designing course in chandigarh.pptx
Graphic designing course in chandigarh.pptxasmeerana605
 
Certificate-Courses-In-Chandigarh.pptx-presentation
Certificate-Courses-In-Chandigarh.pptx-presentationCertificate-Courses-In-Chandigarh.pptx-presentation
Certificate-Courses-In-Chandigarh.pptx-presentationasmeerana605
 
Power BI course in Chandigarh.pptx-presentation
Power BI course in Chandigarh.pptx-presentationPower BI course in Chandigarh.pptx-presentation
Power BI course in Chandigarh.pptx-presentationasmeerana605
 
120 Hours Computer Course.pptx-presentation
120 Hours Computer Course.pptx-presentation120 Hours Computer Course.pptx-presentation
120 Hours Computer Course.pptx-presentationasmeerana605
 
Data Analytics Course in Chandigarh.pptx
Data Analytics Course in Chandigarh.pptxData Analytics Course in Chandigarh.pptx
Data Analytics Course in Chandigarh.pptxasmeerana605
 
Computer courses in Chandigarh Sector 34.pptx
Computer courses in Chandigarh Sector 34.pptxComputer courses in Chandigarh Sector 34.pptx
Computer courses in Chandigarh Sector 34.pptxasmeerana605
 
Marketing training in Chandigarh.pptx-presentation
Marketing training in Chandigarh.pptx-presentationMarketing training in Chandigarh.pptx-presentation
Marketing training in Chandigarh.pptx-presentationasmeerana605
 
Accounting Courses in chandigarh.pptx...
Accounting Courses in chandigarh.pptx...Accounting Courses in chandigarh.pptx...
Accounting Courses in chandigarh.pptx...asmeerana605
 
Cloud Computing Course in Chandigarh.pptx
Cloud Computing Course in Chandigarh.pptxCloud Computing Course in Chandigarh.pptx
Cloud Computing Course in Chandigarh.pptxasmeerana605
 
Artificial intelligence course in Chandigarh.pptx
Artificial intelligence course in Chandigarh.pptxArtificial intelligence course in Chandigarh.pptx
Artificial intelligence course in Chandigarh.pptxasmeerana605
 
Linux training in chandigarh.pptx Join Now
Linux training in chandigarh.pptx Join NowLinux training in chandigarh.pptx Join Now
Linux training in chandigarh.pptx Join Nowasmeerana605
 
Cloud Computing Courses Online.pptx Join Now
Cloud Computing Courses Online.pptx Join NowCloud Computing Courses Online.pptx Join Now
Cloud Computing Courses Online.pptx Join Nowasmeerana605
 
Web Designing training in Chandigarh.pptx
Web Designing training in Chandigarh.pptxWeb Designing training in Chandigarh.pptx
Web Designing training in Chandigarh.pptxasmeerana605
 
DevOps-training-in-chandigarh-Join-now--
DevOps-training-in-chandigarh-Join-now--DevOps-training-in-chandigarh-Join-now--
DevOps-training-in-chandigarh-Join-now--asmeerana605
 
SEO Training in Chandigarh Join Now.pptx
SEO Training in Chandigarh Join Now.pptxSEO Training in Chandigarh Join Now.pptx
SEO Training in Chandigarh Join Now.pptxasmeerana605
 

More from asmeerana605 (20)

Can we use SQL in java.pptx.Join SQL Training in Chandigarh
Can we use SQL in java.pptx.Join SQL Training in ChandigarhCan we use SQL in java.pptx.Join SQL Training in Chandigarh
Can we use SQL in java.pptx.Join SQL Training in Chandigarh
 
Can I learn PHP course in 3 months..pptx
Can I learn PHP course in 3 months..pptxCan I learn PHP course in 3 months..pptx
Can I learn PHP course in 3 months..pptx
 
Best Certificate Courses In Chandigarh.pptx-presentation
Best Certificate Courses In Chandigarh.pptx-presentationBest Certificate Courses In Chandigarh.pptx-presentation
Best Certificate Courses In Chandigarh.pptx-presentation
 
Android course in chandigarh.pptx-presentation
Android course in chandigarh.pptx-presentationAndroid course in chandigarh.pptx-presentation
Android course in chandigarh.pptx-presentation
 
Content Writing Course in Chandigarh.pptx
Content Writing Course in Chandigarh.pptxContent Writing Course in Chandigarh.pptx
Content Writing Course in Chandigarh.pptx
 
Graphic designing course in chandigarh.pptx
Graphic designing course in chandigarh.pptxGraphic designing course in chandigarh.pptx
Graphic designing course in chandigarh.pptx
 
Certificate-Courses-In-Chandigarh.pptx-presentation
Certificate-Courses-In-Chandigarh.pptx-presentationCertificate-Courses-In-Chandigarh.pptx-presentation
Certificate-Courses-In-Chandigarh.pptx-presentation
 
Power BI course in Chandigarh.pptx-presentation
Power BI course in Chandigarh.pptx-presentationPower BI course in Chandigarh.pptx-presentation
Power BI course in Chandigarh.pptx-presentation
 
120 Hours Computer Course.pptx-presentation
120 Hours Computer Course.pptx-presentation120 Hours Computer Course.pptx-presentation
120 Hours Computer Course.pptx-presentation
 
Data Analytics Course in Chandigarh.pptx
Data Analytics Course in Chandigarh.pptxData Analytics Course in Chandigarh.pptx
Data Analytics Course in Chandigarh.pptx
 
Computer courses in Chandigarh Sector 34.pptx
Computer courses in Chandigarh Sector 34.pptxComputer courses in Chandigarh Sector 34.pptx
Computer courses in Chandigarh Sector 34.pptx
 
Marketing training in Chandigarh.pptx-presentation
Marketing training in Chandigarh.pptx-presentationMarketing training in Chandigarh.pptx-presentation
Marketing training in Chandigarh.pptx-presentation
 
Accounting Courses in chandigarh.pptx...
Accounting Courses in chandigarh.pptx...Accounting Courses in chandigarh.pptx...
Accounting Courses in chandigarh.pptx...
 
Cloud Computing Course in Chandigarh.pptx
Cloud Computing Course in Chandigarh.pptxCloud Computing Course in Chandigarh.pptx
Cloud Computing Course in Chandigarh.pptx
 
Artificial intelligence course in Chandigarh.pptx
Artificial intelligence course in Chandigarh.pptxArtificial intelligence course in Chandigarh.pptx
Artificial intelligence course in Chandigarh.pptx
 
Linux training in chandigarh.pptx Join Now
Linux training in chandigarh.pptx Join NowLinux training in chandigarh.pptx Join Now
Linux training in chandigarh.pptx Join Now
 
Cloud Computing Courses Online.pptx Join Now
Cloud Computing Courses Online.pptx Join NowCloud Computing Courses Online.pptx Join Now
Cloud Computing Courses Online.pptx Join Now
 
Web Designing training in Chandigarh.pptx
Web Designing training in Chandigarh.pptxWeb Designing training in Chandigarh.pptx
Web Designing training in Chandigarh.pptx
 
DevOps-training-in-chandigarh-Join-now--
DevOps-training-in-chandigarh-Join-now--DevOps-training-in-chandigarh-Join-now--
DevOps-training-in-chandigarh-Join-now--
 
SEO Training in Chandigarh Join Now.pptx
SEO Training in Chandigarh Join Now.pptxSEO Training in Chandigarh Join Now.pptx
SEO Training in Chandigarh Join Now.pptx
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

what are python basics.pptx.Join Python training in Chandigarh

  • 2. Introduction File handling is the process of reading and manipulating data from files. In Python, file handling is a fundamental skill for working with external data. Python provides a rich set of built-in functions to manage files. Use cases: storing program data, user configurations, logs, and exchanging data with other systems. We will delve into the essentials of file handling using Python. We'll cover concepts such as opening and closing files, reading data, writing content, and different file modes. By the end, you'll have a solid foundation for working with files effectively in your Python projects.
  • 3. Opening and Closing Files Use the open() function to open a file: file_object = open("filename", "mode") Common file modes: 'r' (read), 'w' (write), 'a' (append), 'x' (create) Create new files if they don't exist (with 'w' or 'x') Always close files with file_object.close() The open() function is the gateway to file operations in Python. It creates a file object that offers methods to interact with the file. The mode determines your level of access (read, write, etc). Closing files using close() releases system resources and prevents data corruption.
  • 4. Reading Data from Files The read() method returns the entire content as a string. The readline() method reads a single line at a time. The readlines() method reads all lines into a list of strings. Iterate over files using a for loop for line-by-line processing. Once a file is open in read mode, you have a variety of options to extract data. Choose the method best suited to your task—reading everything at once or working with lines individually. Looping through files allows you to process information systematically.
  • 5. Writing Data to Files Use the write() method to write a string to a file (mode 'w' or 'a'). 'w' mode overwrites existing content, 'a' mode appends to the end. Be careful about overwriting important files. writelines() can write a list of strings to a file. Files allow you to make your program's data persistent. The write() method is your primary tool for saving data to files. Manage how you modify existing files carefully, using either 'w' for overwrites or 'a' to add new content.
  • 6. File Modes 'r' (read): Open for reading (default). Raises an error if the file doesn't exist. 'w' (write): Open for writing, overwrites existing content, or creates a new file. 'x' (create): Creates a new file, fails if the file exists. 'a' (append): Open for writing, appends to the end, or creates if it doesn't exist. 'b' (binary): Used for binary files (images, videos) **'+' (update): ** For both reading and writing. These file modes give you granular control over how you interact with files. Choose the appropriate mode based on whether you need to read existing data, create new data, or update file contents.
  • 7. Working with CSV Files CSV (Comma-Separated Values) stores structured data in plain text. Use Python's csv module for handling CSV files. Import the module: import csv Use csv.reader() to read CSV data into rows. Use csv.writer() to write data into CSV format. CSV is a common format for storing tabular data. Python's csv module provides tools to easily read from and write data to CSV files, making it a great choice for interchanging data between systems.
  • 8. File Path Management Use the os module for file path operations. os.path.exists() checks if a file or directory exists. os.path.join() combines path components correctly. os.path.split() separates the filename from the path. Employ relative and absolute paths for file references. Organizing files and directories is crucial in larger projects. The os module helps navigate your file system—checking if files exist, constructing paths properly, and understanding file locations within your project structure.
  • 9. Error Handling Wrap file operations in try-except blocks. Common file-related exceptions: FileNotFoundError IOError PermissionError Provide meaningful error messages to the user. Ensure files are closed properly with a finally block Files are external resources; things can go wrong (missing files, permissions). Error handling with try-except makes your Python code robust and allows you to provide informative error messages if file operations fail.
  • 10. Conclusion Other Libraries: Explore libraries like pandas for advanced data manipulation. Binary Files: Learn about working with non-text data (images, audio, etc.) Context Managers: Use the with statement for more elegant file handling (with open(...) as file_object). File Handling Best Practices: Emphasize the importance of file handling in data-driven Python projects. These additional points hint at the broader world of file handling. Python offers a rich ecosystem of tools for data-related tasks, allowing you to work with various file formats. Wrap-up the presentation by reiterating the importance of file handling as a fundamental building block for many Python applications.
  • 11. What are Python basics? For Query Contact : 998874-1983