-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_operations.py
58 lines (50 loc) · 1.23 KB
/
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
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
import os.path
import shelve
# path concat
path = os.path.join('/home', 'Users')
print(path)
# current path
path = os.getcwd()
print(path)
# change dir
os.chdir('../')
path = os.getcwd()
print(path)
# recursive create dir
try:
os.makedirs('./tmp/sub')
except FileExistsError:
print('existed')
# relative path to absolute path
abs_path = os.path.abspath('./')
print('', abs_path)
# check if absolute path
print(os.path.isabs(abs_path))
os.chdir('data-processing-basic')
# open file
readme_file = open('./README.md')
# read file
content = readme_file.read()
print(content)
readme_file.close()
print("======")
# read file line
readme_file = open('./README.md')
content_line = readme_file.readlines()
print(content_line)
# write file, coverage
write_file = open('./text.txt', 'w')
write_file.write('hello python')
write_file.close()
# write file, add
write_file = open('./text1.txt', 'a')
write_file.write('hello python\n')
write_file.close()
# shelve save bin file
shelve_file = shelve.open('./text2.txt')
shelve_file['fruit'] = 'apple' # key-value's pattern
shelve_file.close()
# shelve read bin file
shelve_file = shelve.open('./text2.txt')
print(shelve_file['fruit']) # list(shelve_file.keys()) list(shelve_file.values())
shelve_file.close()