-
Notifications
You must be signed in to change notification settings - Fork 1
/
File Handeling part 2.py
64 lines (48 loc) · 1.31 KB
/
File Handeling part 2.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#;==========================================
#; Title: File handeling part 2
#; Author: @AyemunHossain
#;==========================================
#Iterate the file line by line
f=open("story2.txt",'r')
[print(i,end="") for i in f]
f.close()
#Read file and storing as list of lines
file = open("story2.txt","r")
data_list = file.readlines()
file.close()
print(data_list)
#mode function in file :
file = open("story2.txt","r")
if file.mode == 'r':
print("File is open as read mode")
elif file.mode == 'w':
print("File is open as write mode")
elif file.mode == 'a+':
print("Fil is open as append mode")
#try case on file opening
try :
f=open("secret_password.txt", "r")
except:
print("File is not exist ")
else :
pass #further process will done here
finally:
print("__Done__")
string_list = ["I","am","Ashik.","I","read","in","Daffodil","International","University.",]
#Write a sequence to file :
f = open("demo.txt","w")
f.writelines(' '.join(i for i in string_list))
f.close()
seq=['Hello\n','I am Ashik\n','I read in Daffodil International University\n','I live in Dhanmondi\n']
f=open("info.txt",'w')
f.writelines(seq)
f.close()
#check the file
f=open("info.txt",'r')
print(f.read())
f.close()
#Rename the file name:
import os
os.rename("demo.txt","Demo.txt")
#Delete the file:
os.remove("Demo.txt")