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:
- 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
- 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)
- Function processes.
This might make more sense.