Skip to content

Commit 2dd7903

Browse files
committed
File handling and I/O Operations
1 parent 51c0f85 commit 2dd7903

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

file_operations.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
__author__ = 'Avinash'
2+
import os
3+
4+
# Different file attributes
5+
file_attributes = open("test.txt", "wb")
6+
print("Name of the file: ", file_attributes.name)
7+
print("Closed or not : ", file_attributes.closed)
8+
print("Opening mode : ", file_attributes.mode)
9+
file_attributes.close()
10+
11+
# Write to a file in binary format
12+
file_write_bytes = open("test.txt", "wb")
13+
file_write_bytes.write(bytes("Python is a great language.", 'UTF-8'))
14+
file_write_bytes.close()
15+
16+
# Write to a file
17+
file_write = open("test.txt", "w")
18+
file_write.write("Python is a great language.")
19+
file_write.close()
20+
21+
# Reading from a file
22+
file_read = open("test.txt", "r+")
23+
text = file_read.read()
24+
print("Read String is : ", text)
25+
file_read.close()
26+
27+
# Append to a file
28+
file_append = open("test.txt", "a")
29+
file_append .write("\nPython is a powerful language.")
30+
file_append.close()
31+
32+
# Delete a file
33+
os.remove("test.txt")
34+
35+

0 commit comments

Comments
 (0)