0

I need a file in a dir by it's name

I achieved this with

import glob, os
os.chdir("../path")

def search_file(file_to_search):
    for file in glob.glob(file_to_search):
        return file

in another function I am using

with open("./file.csv",'rt')as f:
        lines = list(csv.reader(f))
        for line in lines:
            var = line[1]
            path = search_file(var)

Before I add the search_file function in the second process the file.csv is found. After it is not found anymore. I assume it has to do with the os import?

a work around was this


def find_file(file):
    for root, dirs, files in os.walk('./path'):
        if file in files:
            return os.path.join(root, file)

Does anyone has a solid explanation why with open() would clash with os, if that is the case?

note: the indentation might be wrong, assume the code is running, I got it from functions

Update:

I feel I need to give some more clarity:

This is what I achieve so far with what I have:

  1. Function1 to read a cell from a csv file and if the name in the cell matches the name of a file in a directory, return fill path
  2. Function2 to take parameter(Function1 return ) give the path to json.load(), give the load to json.dump, assign it to key(that expects json format)
  3. Function processes.

This might make more sense.

Scilla
  • 355
  • 2
  • 13
  • "file,csv" is not qualified. Is it in whatever folder you're switching to with the os.chdir("../path") statement? I suggest you use absolute file names, not unqualified names which depend on the current working directory. – Nicholas Hunter Apr 10 '21 at 18:29
  • Thank you, I corrected that. in my code was using absolute path. i think i missed it here – Scilla Apr 11 '21 at 08:01

4 Answers4

0

I tried your code as below:

file.csv has 2 lines as below; file1,file1.txt file2,file2.txt

I have two txt files in path; file1.txt, file2.txt

and I tried your code it worked.

Except in your original code you have for file in glob.glob(file_file_to_search): changed to for file in glob.glob(file_to_search):

import glob, os
    os.chdir("../path")
    
def search_file(file_to_search):
    for file in glob.glob(file_to_search):
        return file

with open("file.csv",'rt')as f:
        lines = list(csv.reader(f))
        for line in lines:
            var = line[1]
            path = search_file(var)
            print(path)

Here is the ouput:

>>> with open("file.csv",'rt')as f:
...         lines = list(csv.reader(f))
...         for line in lines:
...             var = line[1]
...             path = search_file(var)
...             print(path)
...
file1.txt
file2.txt
m.i.cosacak
  • 708
  • 7
  • 21
0

Let`s say you have file.csv in your main folder and your path on Desktop. Still your code works.

import glob, os

file_to_search = "Desktop/path"

def search_file(file_to_search):
    for file in glob.glob(file_to_search):
        return file

with open("file.csv",'rt')as f:
        lines = list(csv.reader(f))
        for line in lines:
            var = line[1]
            var = file_to_search + "/" + var
            path = search_file(var)
            print(path)

Here is code on terminal:

>>> import glob, os
>>>
>>> file_to_search = "Desktop/path"
>>>
>>> def search_file(file_to_search):
...     for file in glob.glob(file_to_search):
...             return file
...
>>> with open("file.csv",'rt')as f:
...             lines = list(csv.reader(f))
...             for line in lines:
...                     var = line[1]
...                     var = file_to_search + "/" + var
...                     path = search_file(var)
...                     print(path)
...
Desktop/path/file1.txt
Desktop/path/file2.txt
m.i.cosacak
  • 708
  • 7
  • 21
  • thank you, it was a typing mistake, changing vars for stack overflow. – Scilla Apr 10 '21 at 19:27
  • just a question, why should i do it your way when my way is shorter? – Scilla Apr 20 '21 at 18:52
  • 1
    There are several solutions, I suggested the one easier for me. As you know your path, `glob.glob` works as I suggested. `os.walk` can be used as well. I generally use it if I do not know dirs and folders in the path. At the end, it does not make difference as long as either way works. – m.i.cosacak Apr 20 '21 at 19:06
  • this is the way. python people. lol. you'd never hear that from a java developer. nice one cosacak. thanks for sharing. I am sure one day i'll come back here – Scilla Apr 20 '21 at 20:11
  • I have same struggle in Java :))) I am asking a lot of simple questions on GitHub or OS. Please find one of them here: https://stackoverflow.com/questions/66976317/imagej-plugins-with-dependencies-or-uberjar-assembly-are-not-available-in-fiji-p – m.i.cosacak Apr 20 '21 at 20:19
0

Try this. It should search for a file, and open it if it is found.

import os
path = os.listdir('./')
newfile = input('Enter the file name: ')
for found in path:
    if str(found) == newfile:
        print(f'{str(found)} matches your search.')
        os.system(f'open {found}')
        break
    else:
        print(f'{str(found)} does not match your search.')
Peter Nielsen
  • 251
  • 4
  • 17
  • what if i would need the root to join the file name when I return it. Say root = ./hello ; fileName = hello1.csv. I need it returned ./hello/hello1.csv I essence what I try to achieve is getting and i have achieve with my code is to: 1. take file name from json, 2.search for that filename in a directory; 3. if the filename is found in that directory return the full pat; 4. pass the return to a json.dump. I am doing all this because I have data that might change and it's easier to keep it separate for the long run, plus ther's only one function processing, instead a function for each json. – Scilla Apr 11 '21 at 07:50
0

if you want get the full path of a file in a folder;

files = glob.glob("./hello/*") 

or search for a pattern; e.g., files ending with csv.

files = glob.glob("./hello/*.csv")

First check if your file is in the list and find the index of the file you are searching for you can get help from this link and if it is in the list.

the_file = files[the_index_of_the_file]

the_file has the full path.

the_file.split("/")[-1] #will give you the file name.
m.i.cosacak
  • 708
  • 7
  • 21