[Python] python 列出目录下的文件夹和文件 →→→→→进入此内容的聊天室

来自 , 2019-11-07, 写在 Python, 查看 108 次.
URL http://www.code666.cn/view/77b83009
  1. #!/usr/bin/env python
  2. #
  3. # [SNIPPET_NAME: List Directories Content]
  4. # [SNIPPET_CATEGORIES: os]
  5. # [SNIPPET_DESCRIPTION: List the content of the home directory]
  6. # [SNIPPET_AUTHOR: Andy Breiner <breinera@gmail.com>]
  7. # [SNIPPET_LICENSE: GPL]
  8. # [SNIPPET_DOCS: http://docs.python.org/library/os.html, http://diveintopython.org/file_handling/os_module.html]
  9.  
  10. import os
  11.  
  12. # expand ~ to /home/<user_name>
  13. # also print out the content of the home directory as a list
  14. print os.listdir(os.path.expanduser("~"))
  15.  
  16. # Loop over all the items and determine if they are a file or directory and
  17. # then print them out
  18. directory = os.path.expanduser("~")
  19. for f in os.listdir(directory):
  20.     if os.path.isfile(os.path.join(directory, f)):
  21.         print "File: " + f
  22.     if os.path.isdir(os.path.join(directory, f)):
  23.         print "Directory: " + f
  24. #//python/2374

回复 "python 列出目录下的文件夹和文件"

这儿你可以回复上面这条便签

captcha