iterate a directory and find only file whose names start with the certain string

user8183713

I have a directory path and in this path there are several folders. So i am am trying to build a script which would find all the xml files and the file name must start with report. I have been so far able to iterate over all the directories but further i do not know how to proceed. Here is my code:

def search_xml_report(rootdir):
     for subdir, dirs, files in os.walk(rootdir):
        for file in files:
            print os.path.join(subdir,file) # print statement just for testing
Vishnu Upadhyay

use str.startswith with os.path.splitext

os.path.splitext: Split the extension from a pathname. Extension is everything from the last dot to the end, ignoring leading dots. Returns "(root, ext)"; ext may be empty.

 if file.startswith('report') and os.path.splitext(filepath+filename)[-1] == '.xml':
     return file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get all the files whose names contain certain pattern in the immediate subdirectory of the current directory without using find?

From Dev

Delete all files in a certain directory that their names start with a certain string in node js

From Dev

Recursively "find" file names containing string and symlink files in another directory

From Dev

change file names in a directory with a certain pattern at the beginning

From Dev

find file starting with a certain string

From Dev

Use of find in unix on strange file/directory names

From Dev

How to iterate through directory and concatenate names of folders into string

From Dev

Find latest file in a directory whose name starts with <> using batch script

From Dev

Unix: finding a string within a directory and listing only its associated file names

From Dev

find string that consists only of a certain set of characters

From Dev

find and echo file names only with pattern found

From Dev

Why is the Range.Find working for only certain column names? VBA

From Dev

List all entries in the current directory whose names contain only uppercase letters

From Dev

Iterate through file names

From Dev

how to replace a string in files only under some folders whose name contains certain string

From Dev

Find a line containing a certain string in a file

From Dev

Find and delete a line that starts with a certain string in a file

From Dev

What are the tables whose names start with `TT_`?

From Dev

How to scan directory and save only image file names to array with php?

From Dev

How to list only the file names and not its attributes in a directory( in Linux)?

From Dev

How to list only the file names of the same type in the directory?

From Dev

Download list of directory/file names only from web server index

From Dev

rsync - copy contents of the directory only if a certain file doesn't exist

From Dev

Fast way to find file names in Linux and specify directory

From Dev

How to find file/directory names that are the same, but with different capitalization/case?

From Dev

linux find file in directory with given string in content

From Dev

Iterate over files in directory, create folders based on file names and move files into respective folders

From Dev

How can I recursively replace a string in file and directory names?

From Dev

Python: how to search for specific "string" in directory name (not individual file names)

Related Related

  1. 1

    How to get all the files whose names contain certain pattern in the immediate subdirectory of the current directory without using find?

  2. 2

    Delete all files in a certain directory that their names start with a certain string in node js

  3. 3

    Recursively "find" file names containing string and symlink files in another directory

  4. 4

    change file names in a directory with a certain pattern at the beginning

  5. 5

    find file starting with a certain string

  6. 6

    Use of find in unix on strange file/directory names

  7. 7

    How to iterate through directory and concatenate names of folders into string

  8. 8

    Find latest file in a directory whose name starts with <> using batch script

  9. 9

    Unix: finding a string within a directory and listing only its associated file names

  10. 10

    find string that consists only of a certain set of characters

  11. 11

    find and echo file names only with pattern found

  12. 12

    Why is the Range.Find working for only certain column names? VBA

  13. 13

    List all entries in the current directory whose names contain only uppercase letters

  14. 14

    Iterate through file names

  15. 15

    how to replace a string in files only under some folders whose name contains certain string

  16. 16

    Find a line containing a certain string in a file

  17. 17

    Find and delete a line that starts with a certain string in a file

  18. 18

    What are the tables whose names start with `TT_`?

  19. 19

    How to scan directory and save only image file names to array with php?

  20. 20

    How to list only the file names and not its attributes in a directory( in Linux)?

  21. 21

    How to list only the file names of the same type in the directory?

  22. 22

    Download list of directory/file names only from web server index

  23. 23

    rsync - copy contents of the directory only if a certain file doesn't exist

  24. 24

    Fast way to find file names in Linux and specify directory

  25. 25

    How to find file/directory names that are the same, but with different capitalization/case?

  26. 26

    linux find file in directory with given string in content

  27. 27

    Iterate over files in directory, create folders based on file names and move files into respective folders

  28. 28

    How can I recursively replace a string in file and directory names?

  29. 29

    Python: how to search for specific "string" in directory name (not individual file names)

HotTag

Archive