- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find a file using Python?
To find a file within a directory using python, you can walk the directory tree using os.walk and find the file as follows −
import os def find_file(file_name, directory_name): files_found =  for path, subdirs, files in os.walk(directory_name): for name in files: if(file_name == name): file_path = os.path.join(path,name) files_found.append(file_path) return files_found find_file('my_file.txt', 'my_folder')
When you run this script and have folder structure like −
my_folder/ another_folder/ my_file another_file hello.py my_file
You'll get the output −
- How to Find Hash of File using Python?
- How to find and replace within a text file using Python?
- How to rename a file using Python?
- How to delete a file using Python?
- How to remove a file using Python?
- How to create a tar file using Python?
- How to create a zip file using Python?
- How to find the file using C#?
- How to find the file using Java?
- How to create a duplicate file of an existing file using Python?
- How to get stat of a file using Python?
- How to get a file system information using Python?
- How to create hardlink of a file using Python?
- How to create softlink of a file using Python?
- How to write binary data to a file using Python?