-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfile_example.py
72 lines (60 loc) · 1.64 KB
/
file_example.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
def open_file(filename: str):
"""
>>> open_file("hello.txt")
No such file or directory: hello.txt
>>> import os
>>> if os.path.exists("../LICENSE"):
... f = open_file("../LICENSE")
... _ = f is not None
... _ = f.readline().strip() == "Apache License"
... _ = f.readline().strip() == "Version 2.0, January 2004"
... f.close()
... _ = f.closed
... with open("../LICENSE") as f:
... _ = f.readline().strip() == "Apache License"
... _ = f.readline().strip() == "Version 2.0, January 2004"
"""
try:
f = open(filename)
return f
except FileNotFoundError:
print(f"No such file or directory: {filename}")
def writefile_append() -> None:
"""
>>> f = open("temp_file1.txt", "a")
>>> f is not None
True
>>> f.writelines("ExampleHub\\n")
>>> f.writelines("Python\\n")
>>> f.close()
>>> f.closed
True
>>>
"""
pass
def writefile_override() -> None:
"""
>>> f = open("temp_file2.txt", "w")
>>> f is not None
True
>>> f.writelines("ExampleHub\\n")
>>> f.writelines("Python\\n")
>>> f.close()
>>> f.closed
True
"""
pass
def delete_file() -> None:
"""
>>> import os
>>> os.path.exists("hello.txt") == False
True
>>> if os.path.exists("example_file.txt"):
... os.remove("example_file.txt")
>>> if os.path.exists("folder_path"):
... os.rmdir("folder_path")
"""
pass
if __name__ == "__main__":
from doctest import testmod
testmod()