Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Python Projects/File Handling/File Handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
An example program to showcase how we can read/write from/to files in python

Objective:
- We have an list of names. Write the names to names.txt file.
- Read the names from names.txt file and now write it to marks.txt file(each person got some marks in exam)

Steps: Read and Write operation
- Open a file
- Do some opertations(Read from it)
- Write to another file
- Close the files


Contributor: Anirban Chand
"""

from random import randint

# Open a file in write mode
in_file = open('names.txt', 'w')

# do something
names = ['Levi', 'Eren', 'Mikasa', 'Hange', 'Erwin', 'Armin', 'Grisha', 'Historia', 'Dina', 'Jean', 'Sasha', 'Hitch', 'Hannes', 'Marlo']
for name in names:
in_file.write(name)
in_file.write('\n')

in_file.close()


# Open a file
in_file = open('names.txt', 'r')
out_file = open('marks.txt', 'w')

# Write the headings
out_file.write('Names')
out_file.write('\t\t \t\t')
out_file.write('Marks\n')
out_file.write('_____')
out_file.write('\t\t \t\t')
out_file.write('_____\n\n')

# Write names their marks in marks.txt file
for name in in_file:
marks = randint(30, 100)
name = name[:-1]
out_file.write(name)
out_file.write('\t\t- \t\t')
out_file.write(str(marks)+'\n')

in_file.close()
out_file.close()
17 changes: 17 additions & 0 deletions Python Projects/File Handling/marks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Names Marks
_____ _____

Levi - 80
Eren - 37
Mikasa - 90
Hange - 93
Erwin - 42
Armin - 72
Grisha - 59
Historia - 51
Dina - 39
Jean - 68
Sasha - 59
Hitch - 80
Hannes - 58
Marlo - 31
14 changes: 14 additions & 0 deletions Python Projects/File Handling/names.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Levi
Eren
Mikasa
Hange
Erwin
Armin
Grisha
Historia
Dina
Jean
Sasha
Hitch
Hannes
Marlo