-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathfile_operations.py
33 lines (27 loc) · 871 Bytes
/
file_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
__author__ = 'Avinash'
import os
# Different file attributes
file_attributes = open("test.txt", "wb")
print("Name of the file: ", file_attributes.name)
print("Closed or not : ", file_attributes.closed)
print("Opening mode : ", file_attributes.mode)
file_attributes.close()
# Write to a file in binary format
file_write_bytes = open("test.txt", "wb")
file_write_bytes.write(bytes("Python is a great language.", 'UTF-8'))
file_write_bytes.close()
# Write to a file
file_write = open("test.txt", "w")
file_write.write("Python is a great language.")
file_write.close()
# Reading from a file
file_read = open("test.txt", "r+")
text = file_read.read()
print("Read String is : ", text)
file_read.close()
# Append to a file
file_append = open("test.txt", "a")
file_append .write("\nPython is a powerful language.")
file_append.close()
# Delete a file
os.remove("test.txt")