If we want to look for the file inside the main directory but also subdirectories, we can use one of the os methods called walk(). Using the os.path module to find files recursively For this, we are going to use the next script. It won’t check whether the file exists in the subdirectory. This code only works for the main directory. We are going to use the wildcard character (*) in the following example.Īll_files = glob.glob('D:\\dir\\file*.*') This code will return all files from D:\dir that start with the word “file”. This is the directory and file tree we are going to use to search for the files: This code will return all existing files (with different extensions) that match the name. For this task, we are going to use the glob function from the glob module. Let’s check if a file exists using just a name without extension. Use name to find files with multiple extensions I already wrote an article on this topic, so you can read it there. The command is going to look like this: D:>python test.py file1.txt Instead of checking if a file exists directly from the script, you can do it from the terminal instead. The isfile() function works only for files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |