Professional Documents
Culture Documents
What is a File?
• Files are named locations on disk to store related information.
• They are used to permanently store data in a non-volatile
memory (e.g. hard disk).
• Since Random Access Memory (RAM) is volatile, we use files for
future use of the data by permanently storing them.
• 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 the resources
that are tied with the file are freed.
File Operations File1 File2
File1
Opening Files
• The key function for working with files is the open() function.
• 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")
f = open("C:/Python38/README.txt")
For Example:
import csv
csv_file = open('/content/drive/MyDrive/Colab Noteb
ooks/Datasets/student_data.csv')
Working with csv files in Python
What is a CSV ?
✓ CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet
or database.
✓ A CSV file stores tabular data (numbers and text) in plain text.
✓ Each line of the file is a data record. Each record consists of one or more fields, separated by commas.
✓ The use of the comma as a field separator is the source of the name for this file format.
✓ For working CSV files in python, there is an inbuilt module called csv.
Sample CSV File (student_data.csv)
csv.reader(csvfile, dialect='excel', **fmtparams)
Return a reader object which will iterate over lines in the given csv file
csv.reader(): read data from a csv file
Using next()
Return the next row of the reader’s iterable object as a list
(if the object was returned from reader()) or a dict (if it is a DictReader instance
writerObject = csv.writer(csvfile)
csv.writer class provides two methods for writing to CSV.
They are
1. writerow() and
2. writerows(). A List of fields
writerow(fields)
A nested List
(List of rows)
writerows(rows)
Python program to demonstrate writing to CSV
import csv # name of csv file
path = "university_records.csv"
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA'] # writing to csv file
with open(path, 'w') as csvfile:
# data rows of csv file # creating a csv writer object
rows = [ ['Nikhil', 'COE', '2', '9.0'], csvwriter = csv.writer(csvfile)
['Sanchit', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'], # writing the fields
['Sagar', 'SE', '1', '9.5'], csvwriter.writerow(fields
['Prateek', 'MCE', '3', '7.8'], # writing the data rows
['Sahil', 'EP', '2', '9.1']] csvwriter.writerows(rows)
7 b) Write a program to find
the frequency of distinct
words in the given text file
and store the words along
with the frequency in a CSV
file.
import csv
path = '/content/drive/MyDrive/Colab Notebooks/Datasets/bhagat.txt'
text_file = open(path)
data =text_file.read().split()
text_file.close()
word_freq=[]
s=set(data)
s=list(s)
for i in s:
word_freq.append(data.count(i))
path = '/content/drive/MyDrive/Colab Notebooks/Datasets/ bhagat.csv'
f=open(path,"w")
write_obj=csv.writer(f)
write_obj.writerow(s)
write_obj.writerow(word_freq)
f.close()