Board logo

标题: [技术讨论] Python脚本获取当前目录下有哪些文件夹 [打印本页]

作者: Python    时间: 2012-3-22 22:44     标题: Python脚本获取当前目录下有哪些文件夹

  1. import os
  2. for i in os.listdir('.'):
  3.     if os.path.isdir(i):
  4.         print i
复制代码
  1. import os
  2. cmd = "dir /b /ad"
  3. for i in os.popen(cmd).readlines():
  4.     print i,
复制代码
  1. import os
  2. for root, dirs, files in os.walk('.'):
  3.     for i in dirs:
  4.         print i
  5.     break
复制代码
试着写了几个,总感觉不够优雅,求大湿指点。
作者: ivor    时间: 2012-4-3 14:25

print 早就更新了,
print(i)
  1. print(...)
  2.     print(value, ..., sep=' ', end='\n', file=sys.stdout)
  3.    
  4.     Prints the values to a stream, or to sys.stdout by default.
  5.     Optional keyword arguments:
  6.     file: a file-like object (stream); defaults to the current sys.stdout.
  7.     sep:  string inserted between values, default a space.
  8.     end:  string appended after the last value, default a newline.
复制代码

作者: Python    时间: 2012-4-3 15:27

回复 2# ivor


你用的Python3是吧,其实还是有很大一部分人在用Python2的,包括我。
作者: Linuxer    时间: 2014-9-30 15:54

学习了,,,我感觉代码已经比较优雅了。。




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2