
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python os.listdir() Method
Description
Python method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Syntax
Following is the syntax for listdir() method −
os.listdir(path)
Parameters
path − This is the directory, which needs to be explored.
Return Value
This method returns a list containing the names of the entries in the directory given by path.
Example
The following example shows the usage of listdir() method.
#!/usr/bin/python import os, sys # Open a file path = "/var/www/html/" dirs = os.listdir( path ) # This would print all the files and directories for file in dirs: print(file)
When we run above program, it produces following result −
test.htm stamp faq.htm _vti_txt robots.txt itemlisting resumelisting writing_effective_resume.htm advertisebusiness.htm papers resume
python_files_io.htm
Advertisements