0

I'm trying to have the bottom part of the code iterate over some files. These files should be corresponding and are differentiated by a number, so the counter is to change the number part of the file.

The file names are generated by looking through the given files and selecting files containing certain things in the title, then having them ordered using the count.

This code works independently, in it's own (lonely) folder, and prints the correct files in the correct order. However when i use this in my main code, where file_1 and file_2 are referenced (the decoder and encoder parts of the code) I get the error in the title. There is no way there is any typo or that the files don't exist because python made these things itself based on existing file names.

import os
count = 201

while 205 > count:
indir = 'absolute_path/models'
for root, dirs, filenames in os.walk(indir):
    for f in filenames:
        if 'test-decoder' in f:
            if f.endswith(".model"):
                if str(count) in f:
                    file_1 = f
                    print(file_1)

indir = 'absolute_path/models'
for root, dirs, filenames in os.walk(indir):
    for f in filenames:
        if 'test-encoder' in f:
            if f.endswith(".model"):
                if str(count) in f:
                    file_2 = f
                    print(file_2)

decoder1.load_state_dict(
    torch.load(open(file_1, 'rb')))

encoder1.load_state_dict(
    torch.load(open(file_2, 'rb')))

print(getBlueScore(encoder1, decoder1, pairs, src, tgt))
print_every=10
print(file_1 + file_2)


count = count + 1

i then need to use these files two by two.

Helen
  • 3
  • 4
  • Perhaps python can't find the relative path you've assigned to the variable `indir`? See if the absolute path (e. g. `C:/Users....`) works. – Joooeey Apr 06 '18 at 22:35
  • it has the absolute path, that's just not public in the question – Helen Apr 06 '18 at 22:37

1 Answers1

0

It's very possible that you are running into issues with variable scoping, but without being able to see your entire code it's hard to know for sure.

If you know what the model files should be called, might I suggest this code:

for i in range(201, 205):
    e = 'absolute_path/models/test_encoder_%d.model' % i
    d = 'absolute_path/models/test_decoder_%d.model' % i
    if os.path.exists(e) and os.path.exists(d):
        decoder1.load_state_dict(torch.load(open(e, 'rb')))
        encoder1.load_state_dict(torch.load(open(d, 'rb')))

Instead of relying on the existence of strings in a path name which could lead to errors this would force only those files you want to open to be opened. Also it gets rid of any possible scoping issues.

We could clean it up a bit more but you get the idea.

guidoism
  • 7,820
  • 8
  • 41
  • 59