Download our e-book of Introduction To Python
Vaibhav Pant
2 years ago
for name in os.listdir('.'):
if os.path.isfile(name):
print ('file: ', name)
elif os.path.isdir(name):
print ('dir: ', name)
elif os.path.islink(name):
(print 'link: ', name)
else:
print('unknown', name)
file: abcl.htm
dir: allbooks
link: ulink
from ftplib import FTP
def main():
ftp = FTP('ftp.ibiblio.org')
ftp.login()
ftp.cwd('pub/academic/biology/') # change to some other subject
entries = ftp.nlst()
ftp.quit()
print(len(entries), "entries:")
for entry in sorted(entries):
print(entry)
if __name__ == '__main__':
main()
(6, 'entries:')
INDEX
README
acedb
dna-mutations
ecology+evolution
molbio
for x in os.listdir('.'):
print x
LICENSE
index.js
API.md
ISSUE_TEMPLATE.md
package.json
test
...
for x in os.listdir('.'):
if os.path.isfile(x): print 'f-', x
elif os.path.isdir(x): print 'd-', x
elif os.path.islink(x): print 'l-', x
else: print '---', x
f- LICENSE
f- index.js
f- API.md
f- ISSUE_TEMPLATE.md
f- package.json
d- test
f- .gitignore
d- .git
f- rollup.config.js
...
print filter(lambda x: os.path.isfile(x), os.listdir('.'))
['LICENSE', 'index.js', 'API.md', 'ISSUE_TEMPLATE.md', 'package.json', '.gitignore', ...]
print filter(lambda x: os.path.isdir(x), os.listdir('.'))
['test', '.git', 'img']
print filter(lambda x: x.endswith('.js'), os.listdir('d3'))
['index.js', 'rollup.config.js', 'rollup.node.js']
print [x for x in os.listdir('.') if x.endswith('.js')]
['index.js', 'rollup.config.js', 'rollup.node.js']
import re
rx = re.compile(r'\.(js|md)')
print filter(rx.search, os.listdir('.'))
['index.js', 'API.md', 'ISSUE_TEMPLATE.md', 'package.json', 'rollup.config.js', ...]
print [x for x in os.listdir('.') if rx.search(x)]
['index.js', 'API.md', 'ISSUE_TEMPLATE.md', 'package.json', 'rollup.config.js', 'CHANGES.md', 'README.md', 'rollup.node
for x in os.walk('.'):
print x
('.', ['test', '.git', 'img'], ['LICENSE', 'index.js', 'API.md', 'ISSUE_TEMPLATE.md', 'package.json', '.gitignore', 'rollup.config.js', 'CHANGES.md', 'd3.sublime-project', 'README.md', 'rollup.node.js', '.npmignore'])
('./test', [], ['test-exports.js', 'd3-test.js'])
('./.git', ['objects', 'logs', 'info', 'refs', 'hooks', 'branches'], ['packed-refs', 'HEAD', 'description', 'config', 'index'])
('./.git/objects', ['pack', 'info'], [])
...
rx = re.compile(r'\.(js|md)')
r = []
for path, dnames, fnames in os.walk('.'):
r.extend([os.path.join(path, x) for x in fnames if rx.search(x)])
print r
['./index.js', './API.md', './ISSUE_TEMPLATE.md', './package.json', './rollup.config.js', './CHANGES.md', './README.
r = []
for path, dnames, fnames in os.walk('.'):
r.extend([os.path.join(path, x) for x in fnames if os.path.getsize(os.path.join(path, x)) > 10000])
print r
r = []
now = int(time.time())
for path, dnames, fnames in os.walk('.'):
r.extend([os.path.join(path, x) for x in fnames if (now - int(os.path.getmtime(os.path.join(path, x))))/(60*60) < 8])
print r