Python: Print all files under a path

Source: Internet
Author: User


To print all the files under a path, we can do this with the Find command (find path-type f). Now I'm using Python's recursive return to implement this function.

[Email protected] ~]# vim print_files.py

#!/usr/bin/python


Import OS

Import Sys


def print_files (path):

Lsdir = Os.listdir (path)

dirs = [i-I in Lsdir if Os.path.isdir (Os.path.join (path,i))]

Files = [i-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])



Test:

We print all directories and files under the/etc/directory, with the following effects:

[email protected] ~]# python print_files.py/etc/

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/80/19/wKiom1c3PRLgVG1rAAA6GmB9uug525.png "title=" Qq20160514225704.png "alt=" Wkiom1c3prlgvg1raaa6gmb9uug525.png "/>


Method Description:

Os.listdir: OS module method, print all the files in the directory, as a list, can be used after the list rewrite: [I for I in Lsdir]

Os.path.isdir: The Os.path method, which returns a Boolean value, determines whether a directory is behind.

Os.path.isfile: Os.path The method below, the return is also a Boolean value, judging behind is not a file.

Os.path.join: Os.path The method, connect the file path. Os.path.join (path,i):/etc/i

SYS.ARGV: sys module , which is used to get command-line arguments, Sys.argv[0] represents the file path of the code itself, in this case:

Sys.argv[0]------- Print_files . PY

SYS.ARGV[1]-------Path


Logical thinking:

(1) First list all files under the path

(2) Filter catalogue

(3) Filter files

(4) Determine whether there are files in the directory, if any, print out the file

(5) Determine whether there are subdirectories under the directory, if any, through recursion and then traverse the subdirectories of the file or directory.




This article is from the "M April Days" blog, please be sure to keep this source http://msiyuetian.blog.51cto.com/8637744/1773443

Python: Print all files under a path

Related Article

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.