In [1]: import os
In [2]: os.listdir(’/home’)
Out[2]: []
In [3]: os.listdir(’/home’)
Out[3]: [‘gltest’]
In [4]: os.listdir(’.’)
Out[4]:
[‘11.py’,
‘.pydistutils.cfg’,
‘.cache’,
‘7.py’,
‘8.py’,
‘.ipython’,
‘13.py’,
‘130.txt’,
‘133.py’,
‘.cshrc’,
‘1.pyc’,
‘.bash_profile’,
‘software’,
‘.9.py.swp’,
‘mac.py’,
‘3.py’,
‘.ssh’,
‘.bashrc’,
‘5.py’,
‘myprojects’,
‘1.pyo’,
‘.bash_logout’,
‘10.py’,
‘131.txt’,
‘duihua.py’,
‘.viminfo’,
‘132.txt’,
‘4.py’,
‘6.py’,
‘.pki’,
‘12.py’,
‘2.py’,
‘1.py’,
‘.pip’,
‘.bash_history’,
‘.tcshrc’]
In [5]: os.path.isdir(’/home’)
Out[5]: True
In [6]: os.path.isdir(’/ace’)
Out[6]: False
In [7]: os.path.join(’/etc’,‘passwd’,‘abc’)
Out[7]: ‘/etc/passwd/abc’
#!/usr/bin/python
import os
import sys
def print_files(path):
lsdir=os.listdir(path)
dirs= [i for i in lsdir if os.path.isdir(os.path.join(path,i))]
files =[i for i in lsdir if os.path.isfile(os.path.join(path,i))]
if dirs:
for d in dirs:
print_files(os.path.join(path,d))
if files:
for f in files:
print os.path.join(path,f)
print_files(sys.argv[1])
匿名函数
In [2]: def fun(x,y):
…: return x*y
…: fun(3,5)
…:
Out[2]: 15
In [3]: lambda x,y:x*y
Out[3]: <function main.>
In [4]: r=lambda x,y:x*y
In [5]: r(3,5)
Out[5]: 15
In [6]:
In [2]: def fun(x,y):
…: return x*y
…: fun(3,5)
…:
Out[2]: 15
In [3]: lambda x,y:x*y
Out[3]: <function main.>
In [4]: r=lambda x,y:x*y
In [5]: r(3,5)
Out[5]: 15
In [6]: