1

i recently just took up python for my research studentship (so i don't have a very strong cs background). I'm dealing with a large set of image files in many different subfolders in a big folder so I want to build a python code to search and open them.

I got introduced to os and sys libraries to play around with them. I could get the file to open but that is only when I specifically put a full dirpath for the file. I'm having trouble building a code to direct python to the right folder when I only know the folder name(i'm not sure if i'm making sense haha sorry).

My goal is to be able to type the id name of a folder containing the image in the python output so the file could be pulled out and displayed.

Any suggestions would be great! thank you so much!

Matcha38
  • 11
  • 1
  • `os`is the way to go! I'd look into `os.listdir` and `os.path.abspath` - for instance `os.path.abspath(os.listdir(my_folder)[0])` will pull out the path to one of the files in my_folder. – Seon Jun 07 '21 at 15:01
  • i see i will try that thank you so much! – Matcha38 Jun 08 '21 at 00:13
  • @Seon hi! I was wondering if there's any ways that I can choose my own file instead of just the first file (like how you wrote [0]) above? what can I use in this case? – Matcha38 Jun 08 '21 at 04:06
  • Could you be please be more specific? When you say "choose my own file", are you looking for an interface that shows you all files and lets you pick one? Do you know its extension/part of its name and want to select it automatically from that? Or maybe something else completely? – Seon Jun 08 '21 at 07:07
  • Your Operating System probably already has some tools to help you do that, or there is softwares to do it already. If you prefer to code something yourself, @Matcha38 is right, you should expand on how you plan to use it and try to code it. There exist many tutorials online to learn how to work with files in Python. – Lenormju Jun 08 '21 at 11:31
  • @Seon hi sorry for the confusion. What I meant was to be able to select and open a particular file from windows explorer. I know their paths but don't know how to write a code to automate the file opening. – Matcha38 Jun 13 '21 at 20:13
  • For example i have this 3 png files a,b,c in the same dirpath C:/abc/openfiles/...png. "...png" is where a,b, or c supposed to be. I want to open file a, b, and c separately without having to retyping the path in the input everytime. Does that make sense? – Matcha38 Jun 13 '21 at 20:17
  • @Lenormju Thank you! Do you have any recommendations for reliable tutorials/sources? – Matcha38 Jun 13 '21 at 20:18
  • @Matcha38 ```os.path.abspath(os.listdir(my_folder)``` should contain all files in `my_folder`. If you're only interested in files with the png extension, you can either filter your list to only keep those, or take a look at the `glob` extension. – Seon Jun 13 '21 at 21:03
  • thank you so much! I will look into that! – Matcha38 Jun 14 '21 at 01:25

1 Answers1

1

You should look at the documentation for the functions we recommended you in the comments. Also, you may be interested to read some tutorials on files and directory, mainly in Python.
And look at how many questions we had to ask you to understand what you wanted to do. Provide code. Explain clearly what is your input, its type, its possible values, and what is the expected output.

Anyway, from what I understood so far, here is a proposal based on os.startfile :

import os
from pathlib import Path

# here I get the path to the desired directory from user input, but it could come from elsewhere
path_to_directory = Path(input("enter the path to the folder : "))

extension_of_interest = ".jpg"
filepaths_of_interest = []

for entry in path_to_directory.iterdir():
    if entry.is_file() and entry.name.endswith(extension_of_interest):
        print("match: " + str(entry))
        filepaths_of_interest.append(entry)
    else:
        print("ignored: " + str(entry))

print("now opening ...")
for filepath_of_interest in filepaths_of_interest:
    os.startfile(filepath_of_interest, "open")

when run, given the path C:/PycharmProjects/stack_oveflow/animals, it prints :

enter the path to the folder : C:/PycharmProjects/stack_oveflow/animals
ignored: C:\PycharmProjects\stack_oveflow\animals\cute fish.png
match: C:\PycharmProjects\stack_oveflow\animals\cute giraffe.jpg
match: C:\PycharmProjects\stack_oveflow\animals\cute penguin.jpg
match: C:\PycharmProjects\stack_oveflow\animals\cute_bunny.jpg
now opening ...

and the 3 jpg images have been opened with my default image viewer.

The startfile function was asked to "open" the file, but there are other possibilities described in the documentation.

Lenormju
  • 4,078
  • 2
  • 8
  • 22