Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find a file in specific folders in Python

I have 5 folders, 1,2,3,4,5. I want to know which folders don’t have the file Data.csv in them. Is there a straightforward way to do it?

N=5
for i in range(1,N+1):
    file_loc = f"C:\\Users\\{i}\\Data.csv"

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use os.path.exists to check if the provided path exists or not. Also on a side note, instead of manually adding \\ in the path, you can use os.path.join

import os
N=5
for i in range(1,N+1):
    file_loc = os.path.join("C:\\", "Users", str(i), "Data.csv")
    if not os.path.exists(file_loc):
        print(i)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading