Skip to content
Merged
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
52 changes: 48 additions & 4 deletions file_handling.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from os import read
import os

### checkout python docs for more https://docs.python.org/3/library/filesys.html
### file = open('filename' , 'mode')
"""
modes = r (read only)
r+ (read and write)
w (write) ## the W mode overwrites the entire file.....so beware
a (append) ## append mode appends text at the end of file
x (create)
t (text)
b (binary)
"""

# file reading
file = open('trial.txt' , 'r')
file = open('trial.txt' , 'r+')

for each in file:
print(each)
Expand All @@ -10,14 +23,45 @@

# file writing

file.write("this is the write function")
file.write("this lets us write to the file")
file.write("this is the write function\n")
file.write("this lets us write to the file\n")
file.close # the close function lets us close all the resources in use


## Append mode

file = open('trial.txt','a')

file.write("writing this in append mode")
file.write("writing this in append mode\n")
file.close

## using write with with()

with open('trial.txt','r+') as file:
text = file.read()
file.write("writing from within with()\n")

## creating a file using x mode

file2 = open('newfile','x') ## creates the file ...returns error if the file already exists
os.remove("newfile") ## this removes the file

## readline()

file3 = open('trial.txt')
print(file3.readline())
print(file3.readline()) ## this would print the first two lines of the file

## create a file , add a line and a string , read them , split the string and store it in a list , delete the file by checking if it exists atfirst

file4 = open("myfile","x")
file4 = open("myfile","r+")
file4.write("this is the line\n")
file4.write("apple , banana , cherry , mango")
content = file4.read()
print(content) #TODO debug here...

if os.path.exists("myfile"):
os.remove("myfile")
else:
print("the file doesnt exist\n")
1 change: 1 addition & 0 deletions trial.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
helloo brooo
this is just a simple text file for testing purposes