Recursively list all files in a directory

Source: Internet
Author: User

The method to use:

Import OS

Os.listdir ('/etc/'): Lists all files and directories under the specified directory

Os.path.isdir ('/etc/'): Determines whether it is a directory, returns a Boolean value

Os.path.isfile ('/etc/passwd '): Determines whether a file is returned with a Boolean value

Os.path.join ('/etc ', ' passwd '): Connect two paths

Cases:


#!/usr/bin/python


Import Sys

Import OS


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 files:

For f in Files:

Print Os.path.join (path, F)

If dirs:

For D in dirs:

Print_files (Os.path.join (path, D))


Print_files (Sys.argv[1])


This article is from "The Miracle Teenager" blog, please be sure to keep this source http://raffaelexr.blog.51cto.com/8555551/1783018

Recursively list all files in a directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.