sfetan
sfetan

Reputation: 109

How to call a subdirectory of the current working directory correctly with glob

I want to search all files in a subdirectory of my current working directory, this is part of a bigger code where I choose different subfolders and make some manupulations of files in them. But I don't want to call the whole file path each time.:

import glob
import os

os.chdir("C:/Users/John")
for i in glob.glob("/subfolder/**"):
   print(i)

So if there is something in subfolder this code should print it. But somehow glob.glob can't finde the folder

Upvotes: 0

Views: 1638

Answers (2)

Aditi Sharma
Aditi Sharma

Reputation: 493

You have added / in front of subfolder which is not directly accessible. Try removing it. This code works for me.

import glob
import os

os.chdir("C:/Users/John")
for i in glob.glob("subfolder/**"):
   print(i)

Upvotes: 1

LeMorse
LeMorse

Reputation: 132

I don't understand...

You want display the files of your subfolder ?

import glob
import os

os.chdir("C:/Users/John")
for e in glob.glob("./your_subfolder_name/*"):
   print(e)

This display all the files in the folder :

C:/Users/John/your_subfolder_name

Upvotes: 0

Related Questions