-
Notifications
You must be signed in to change notification settings - Fork 1
/
#27_file_handling.py
142 lines (108 loc) · 3.12 KB
/
#27_file_handling.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
path = "/home/ishtiaq/Documents/Python_Files/test.txt"
# file detection
if os.path.exists(path):
print("That location exists")
if os.path.isfile(path):
print("This is a file")
elif os.path.isdir(path):
print("That is a directory")
else:
print("The location doesn't exist")
# file read
try:
with open('test.txt') as file:
print(file.read())
except FileNotFoundError as e:
print(e)
print("That file was not found")
finally:
if file.closed:
print("The file is closed")
else:
file.close()
# file write and append
python = "Python is fun!"
python_append = "\nI love python"
# write
try:
with open('python.txt', 'w') as file:
file.write(python)
with open('python.txt') as file:
print(file.read())
except FileNotFoundError as e:
print(e)
print("That file was not found")
finally:
if file.closed:
print("The file is closed")
else:
file.close()
# append
try:
with open('python.txt', 'a') as file:
file.write(python_append)
with open('python.txt') as file:
print(file.read())
except FileNotFoundError as e:
print(e)
print("That file was not found")
finally:
if file.closed:
print("The file is closed")
else:
file.close()
# copyfile() = copies content of a file
# copy() = copyfile() + permission mode + destination can be a directory
# copy2() = copy() + copies metadata (file's creation and modification times)
import shutil
shutil.copyfile('test.txt', '/home/ishtiaq/Documents/copy.txt') # src, destination
shutil.copy('test.txt', '/home/ishtiaq/Documents/copy.txt') # src, destination
shutil.copy2('test.txt', 'copy.txt') # src, destination
# move file & folder
# file
source_file = "copy.txt"
destination_file = "/home/ishtiaq/Documents/"
try:
if os.path.exists(destination_file):
print("There is already a file there")
else:
os.replace(source_file, destination_file)
print(source_file + " is moved")
except FileNotFoundError:
print(source_file + " was not found")
# folder
source_folder = "folder"
destination_folder = "/home/ishtiaq/Documents/folder"
try:
if os.path.exists(destination_folder):
print("There is already a folder there")
else:
os.replace(source_folder, destination_folder)
print(source_folder + " is moved")
except FileNotFoundError:
print(source_folder + " was not found")
# Delete file & folder
# file
path_delete = 'copy.txt'
try:
os.remove(path_delete)
print(path_delete + " is deleted")
except FileNotFoundError:
print("That file was not found")
else:
print(path_delete + " is deleted")
# folder
path_folder_delete = 'python_folder'
try:
os.rmdir(path_folder_delete) # empty folder
# shutil.rmtree(path_folder_delete) # non-empty file
print(path_folder_delete + " is deleted")
except FileNotFoundError:
print("Yjay file was not found")
except PermissionError:
print("YOu do not have permission to delete that")
except OSError:
print("You cann't delete that using that function")
else:
print(path_folder_delete + " is deleted")