forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathospath.py
More file actions
70 lines (59 loc) · 1.99 KB
/
ospath.py
File metadata and controls
70 lines (59 loc) · 1.99 KB
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
# encoding: utf-8
"""
Topic: sample
Desc:
"""
import os
__author__ = 'Xiong Neng'
def main():
for tmpdir in ('/tmp', r'c:\temp'):
if os.path.isdir(tmpdir):
print('find tmpdir:', tmpdir)
break
else:
print('no temp dir available')
tempdir = ''
if tmpdir:
os.chdir(tmpdir)
cwd = os.getcwd()
print('*** current temporary directory:')
print(cwd)
print('-------')
os.mkdir('example')
os.chdir('example')
cwd = os.getcwd()
print('now...', cwd)
print('list dir:', os.listdir(cwd))
print('----create test file-----')
fobj = open('test.txt', 'w')
fobj.write('this is a line....\n')
fobj.write('second line....\n')
fobj.close()
print('--------now again list...-------')
print(os.listdir(cwd))
print('----------rename----------')
os.rename('test.txt', 'new_test.txt')
print('----------after rename-------')
print(os.listdir(cwd))
path = os.path.join(cwd, os.listdir(cwd)[0])
print('join,,,,full file path is :', path)
print('---(filepath, basename)---', os.path.split(path))
print('====(filename, extension====', os.path.splitext(os.path.basename(path)))
print('-----display file contents----')
fobj = open(path)
for eachline in fobj:
print(eachline),
fobj.close()
print('-----------delete test file---------')
os.remove(path)
print('-----------udpated directory listing:----')
print(os.listdir(cwd))
os.chdir(os.pardir)
print('------after change dir to the parent dir------')
print('now list dirs:', os.listdir(os.getcwd()))
print('-------delete test directory---------')
os.rmdir('example')
print('now list dirs:', os.listdir(os.getcwd()))
print('=========================END======================')
if __name__ == '__main__':
main()