I am trying to find a list of strings in a directory of files. Here is my code:

# -*- coding: utf-8 -*-
import os
import fileinput

s2 = os.listdir('/home/malikarumi/Projects/P5/shortstories')

with open('/home/malikarumi/Projects/P5/list_stories') as f:
    lines = f.readlines()

for line in lines:
     for item in fileinput.input(s2):
         if line in item:
            with open(line + '_list', 'a+') as l:
                l.append(filename(), filelineno(), line)

And here is my error message:

In [44]: %run algo_e3.py
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/home/malikarumi/Projects/Pipeline/4 Transforms/algo_e3.py in <module>()
      9 
     10 for line in lines:
---> 11      for item in fileinput.input(s2):
     12          if line in item:
     13             with open(line + '_list', 'a+') as l:

/usr/lib/python3.4/fileinput.py in __next__(self)
    261             self._filelineno += 1
    262             return line
--> 263         line = self.readline()
    264         if not line:
    265             raise StopIteration

/usr/lib/python3.4/fileinput.py in readline(self)
    360                         self._file = self._openhook(self._filename, 
self._mode)
    361                     else:
--> 362                         self._file = open(self._filename, self._mode)
    363         self._buffer = self._file.readlines(self._bufsize)
    364         self._bufindex = 0

FileNotFoundError: [Errno 2] No such file or directory: 'THE LAND OF LOST TOYS~'

In trying to figure out what is wrong. I have run this same code, down to the 
'for line in lines', but ending with a print statement, and that works fine. I 
am also able to get it to print a list of the names of the files in s2. But as 
soon as I add 'for file in fileinput....' things go sideways. Clearly the files 
exist, and I gave a full path. 

I see the pointers in the traceback, but I don't really grasp what they are 
telling me to do. 'line = self.readline()' and 'self._file = 
open(self._filename...' almost seems like fileinput is reading an instance of 
itself instead of s2.  I don't think that's right but it would explain why it 
can't find the file. 

I don't know what the tilde at the end of 'The Land of Lost Toys' is about. I 
don't think capitalization is an issue. All the files have all cap names to 
help me debug this from my list of strings. 

And if there is no such file or directory, how does Python know the correct 
name of at least this one file? My code does not give the names, all that is in 
the assigned variables. 

Thanks for your help. 
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to