Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92549
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-06-12 12:39 -0700 |
| Message-ID | <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com> (permalink) |
| Subject | Path, strings, and lines |
| From | Malik Rumi <malik.a.rumi@gmail.com> |
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.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 12:39 -0700
Re: Path, strings, and lines Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-12 14:30 -0600
Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:48 -0700
Re: Path, strings, and lines MRAB <python@mrabarnett.plus.com> - 2015-06-13 19:25 +0100
Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-15 19:00 -0700
Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-15 20:44 -0600
Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' MRAB <python@mrabarnett.plus.com> - 2015-06-16 03:51 +0100
Re: Path, strings, and lines Chris Angelico <rosuav@gmail.com> - 2015-06-13 09:47 +1000
Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:50 -0700
csiph-web