-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathospathex.py
More file actions
72 lines (63 loc) · 1.83 KB
/
ospathex.py
File metadata and controls
72 lines (63 loc) · 1.83 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
71
72
#!/usr/bin/env python
# coding:utf-8
'''
欢迎参加黄哥python远程视频培训,
帮你完成从不会写代码到会写代码解决问题的过渡。
https://github.com/pythonpeixun/article/blob/master/index.md
咨询qq:1465376564
黄哥Python培训 黄哥改写
Python 3
P201页代码
'''
import os
for tmpdir in ('/tmp', 'c:/windows/temp'):
if os.path.isdir(tmpdir):
break
else:
print('no temp directory available')
tmpdir = ''
if tmpdir:
os.chdir(tmpdir)
cwd = os.getcwd()
print('*** current temporary directory')
print(cwd)
print('*** creating example directory...')
os.mkdir('example')
os.chdir('example')
cwd = os.getcwd()
print('*** new working directory:')
print(cwd)
print('*** original directory listing:')
print(os.listdir(cwd))
print('*** creating test file...')
file = open('test', 'w')
file.write('foo\n')
file.write('bar\n')
file.close()
print('*** updated directory listing:')
print(os.listdir(cwd))
print("*** renaming 'test' to 'filetest.txt'")
os.rename('test', 'filetest.txt')
print('*** updated directory listing:')
print(os.listdir(cwd))
path = os.path.join(cwd, os.listdir(cwd)[0])
print('*** full file pathname:')
print(path)
print('*** (pathname, basename) == ')
print(os.path.split(path))
print('*** (filename, extension) == ')
print(os.path.splitext(os.path.basename(path)))
print('*** displaying file contents:')
file = open(path)
allLines = file.readlines()
file.close()
for eachLine in allLines:
print(eachLine, end=",")
print('*** deleting test file')
os.remove(path)
print('*** updated directory listing:')
print(os.listdir(cwd))
os.chdir(os.pardir)
print('*** deleting test directory')
os.rmdir('example')
print('*** DONE')