-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathospathex.py
61 lines (53 loc) · 1.57 KB
/
ospathex.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
#!/usr/bin/env python
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,
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'