System Tools(Chapter 2 of Programming Python)
1 def more(text, numlines=15):
2 lines = text.splitlines()
3 while lines:
4 chunk = lines[:numlines]
5 lines = lines[numlines:]
6 for line in chunk:
7 print line
8 if lines and raw_input('More?') not in ['y', 'Y']:
9 break
10
11 if __name__ == '__main__':
12 import sys
13 more(open(sys.argv[1]).read(), 10)
14
2 lines = text.splitlines()
3 while lines:
4 chunk = lines[:numlines]
5 lines = lines[numlines:]
6 for line in chunk:
7 print line
8 if lines and raw_input('More?') not in ['y', 'Y']:
9 break
10
11 if __name__ == '__main__':
12 import sys
13 more(open(sys.argv[1]).read(), 10)
14
1 >>> line = 'aaa\nbbb\nccc\n'
2 >>> line.split('\n')
3 ['aaa', 'bbb', 'ccc', '']
4 >>> line.splitlines()
5 ['aaa', 'bbb', 'ccc']
6 >>> sys.platform
7 'win32'
8 >>> sys.path
9 ['C:\\Python27\\Lib\\idlelib', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\virtualenv-1.5.1-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
10 >>> os.getcwd()
11 'C:\\Python27'
12 >>> os.system('dir')
13 0
14 >>> os.system('dir /b')
15 0
16 >>> os.system('java')
17 0
18 >>> s = os.popen('dir /B').readlines()
19 >>> s
20 ['DLLs\n', 'Doc\n', 'ez_setup.py\n', 'include\n', 'Lib\n', 'libs\n', 'LICENSE.txt\n', 'NEWS.txt\n', 'python.exe\n', 'pythonw.exe\n', 'README.txt\n', 'Scripts\n', 'tcl\n', 'Tools\n', 'w9xpopen.exe\n']
2 >>> line.split('\n')
3 ['aaa', 'bbb', 'ccc', '']
4 >>> line.splitlines()
5 ['aaa', 'bbb', 'ccc']
6 >>> sys.platform
7 'win32'
8 >>> sys.path
9 ['C:\\Python27\\Lib\\idlelib', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\virtualenv-1.5.1-py2.7.egg', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
10 >>> os.getcwd()
11 'C:\\Python27'
12 >>> os.system('dir')
13 0
14 >>> os.system('dir /b')
15 0
16 >>> os.system('java')
17 0
18 >>> s = os.popen('dir /B').readlines()
19 >>> s
20 ['DLLs\n', 'Doc\n', 'ez_setup.py\n', 'include\n', 'Lib\n', 'libs\n', 'LICENSE.txt\n', 'NEWS.txt\n', 'python.exe\n', 'pythonw.exe\n', 'README.txt\n', 'Scripts\n', 'tcl\n', 'Tools\n', 'w9xpopen.exe\n']